无法解析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)
})
}
为什么在Swift中使用NSDictionary,NSArray等? – Alexander
@Ali嗨。我很抱歉,但你的问题仍然不清楚。我可以建议,如果你不确定如何搜索,你可以通过浏览[我的JSON答案](http://stackoverflow.com/search?tab=votes&q=user%3a2227743%20json)开始?有很多,并且所有相关链接都会给你足够的材料来解决你的问题 - 或者在阅读完一段后能够更好地解释你的问题。祝你好运! – Moritz
@EricAya即时通讯让我更新5分钟 – Ali