如何在此SQL数据中使用NSArray和NSDictionary?
问题描述:
无法解析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)
})
}
答
注: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)
})
}
答
问题是JSONSerialization
返回一个不可变的数组,你不能只是投了NSArray
到NSMutableArray
。相反,只是声明它是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的本地集合类型,而不是回落旧NSArray
和NSDictionary
类型:
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
。)
为什么在Swift中使用NSDictionary,NSArray等? – Alexander
@Ali嗨。我很抱歉,但你的问题仍然不清楚。我可以建议,如果你不确定如何搜索,你可以通过浏览[我的JSON答案](http://*.com/search?tab=votes&q=user%3a2227743%20json)开始?有很多,并且所有相关链接都会给你足够的材料来解决你的问题 - 或者在阅读完一段后能够更好地解释你的问题。祝你好运! – Moritz
@EricAya即时通讯让我更新5分钟 – Ali