2016-12-02 27 views
0

无法解析JSON数据。如何在此SQL数据中使用NSArray和NSDictionary?

无法将类型'__NSArrayI'(0x10f02fc08)的值转换为'NSMutableArray'(0x10f02fcd0)。

JSON数据here和代码在这里;

echo json_encode($resultArray); 

这里是我的试用码;

func parseJSON() { 
    var jsonResult: NSMutableArray = NSMutableArray() 

    do{ 
     jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSMutableArray 
    } catch let error as NSError { 
     print(error) 
    } 

    var jsonElement: NSDictionary = NSDictionary() 
    let locations: NSMutableArray = NSMutableArray() 

    for i in 1...jsonResult.count 
    { 
     jsonElement = jsonResult[i-1] as! NSDictionary 

     let location = LocationModel() 

     if let name = jsonElement["Name"] as? String, 
      let address = jsonElement["Address"] as? String, 
      let latitude = jsonElement["Latitude"] as? String, 
      let longitude = jsonElement["Longitude"] as? String 
     { 
      location.name = name 
      location.address = address 
      location.latitude = latitude 
      location.longitude = longitude 

     } 
     locations.add(location) 
    } 
    DispatchQueue.main.async(execute: {() -> Void in 

     self.delegate.itemsDownloaded(items: locations) 

    }) 
} 
+2

为什么在Swift中使用NSDictionary,NSArray等? – Alexander

+0

@Ali嗨。我很抱歉,但你的问题仍然不清楚。我可以建议,如果你不确定如何搜索,你可以通过浏览[我的JSON答案](http://stackoverflow.com/search?tab=votes&q=user%3a2227743%20json)开始?有很多,并且所有相关链接都会给你足够的材料来解决你的问题 - 或者在阅读完一段后能够更好地解释你的问题。祝你好运! – Moritz

+0

@EricAya即时通讯让我更新5分钟 – Ali

回答

0

注:IT将修复THIS

代替:

var jsonResult: NSMutableArray = NSMutableArray() 

做到这一点:

var jsonResult: NSArray = NSArray() 

,而不是和:

jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSMutableArray 

做到这一点:

jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray 

全码:

func parseJSON() { 
    var jsonResult: NSArray = NSArray() 
    do{ 
     jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray 
    } catch let error as NSError { 
     print(error) 
    } 

    var jsonElement: NSDictionary = NSDictionary() 
    let locations: NSMutableArray = NSMutableArray() 

    for i in 1...jsonResult.count 
    { 
     jsonElement = jsonResult[i-1] as! NSDictionary 

     let location = LocationModel() 

     if let name = jsonElement["Name"] as? String, 
      let address = jsonElement["Address"] as? String, 
      let latitude = jsonElement["Latitude"] as? String, 
      let longitude = jsonElement["Longitude"] as? String 
     { 

      location.name = name 
      location.address = address 
      location.latitude = latitude 
      location.longitude = longitude 

     } 

     locations.add(location) 

    } 
    DispatchQueue.main.async(execute: {() -> Void in 

     self.delegate.itemsDownloaded(items: locations) 

    }) 
} 
0

问题是JSONSerialization返回一个不可变的数组,你不能只是投了NSArrayNSMutableArray。相反,只是声明它是NSArray

func parseJSON() { 
    var jsonResult: NSArray 

    do { 
     jsonResult = try JSONSerialization.jsonObject(with: data as Data) as! NSArray 
    } catch let error as NSError { 
     print(error) 
     return 
    } 

    ... 
} 

,或者,使用SWIFT的本地集合类型,而不是回落旧NSArrayNSDictionary类型:

func parseJSON() { 
    var jsonResult: [[String: Any]] // this is an array of dictionaries 

    do { 
     jsonResult = try JSONSerialization.jsonObject(with: data as Data) as! [[String: Any]] 
    } catch let error as NSError { 
     print(error) 
     return 
    } 

    for jsonElement in jsonResult {    
     if let name = jsonElement["Name"] as? String, 
      let address = jsonElement["Address"] as? String, 
      let latitude = jsonElement["Latitude"] as? String, 
      let longitude = jsonElement["Longitude"] as? String 
     { 
      let location = LocationModel() 
      location.name = name 
      location.address = address 
      location.latitude = latitude 
      location.longitude = longitude 
      locations.add(location) // or, if you defined `locations` to be a Swift array, `locations.append(location)` 
     } 
    } 
    ... 
} 

(就个人而言,我可能会还定义了data是一个Data而非NSData并定义locations[LocationModel]而非NSMutableArray。)