我有一个NodeJS API,用于查询MongoDB集群并以下列格式返回附近的数据。问题是我无法使用Swift 3和XCode 8.1枚举位置。
[ { " _id":" 57b03fa32c6835946afd358c", " location":{ "坐标":[ 144.2, -34 ] "海拔":" 1", "输入":"点" }, " name":"资源01", " url":" http://www.url.com/Resource01.html" }, { " _id":" 34b03fa32c6835946afd358c", " location":{ "坐标":[ 154.2, -35.3 ] "海拔":" 1", "输入":"点" }, "名称":"资源02", " url":" http://www.url.com/Resource02.html" } ]
这是我的查询代码。
let json = try! JSONSerialization.jsonObject(with: data)
print(json)
if let statusesArray = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: AnyObject]],
let jsonbranch = statusesArray?[0] as? [String: AnyObject],
let therecord = jsonbranch["_id"] as? String {
print ("_id: " + therecord)
}
我尝试使用SwiftyJSON,但它也不起作用?
答案 0 :(得分:0)
解决:
if let jsonBranch = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: AnyObject]], let name = jsonBranch?[0]["name"] as? String { print ("name: " + name) } if let jsonBranch = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: AnyObject]], let name = jsonBranch?[1]["name"] as? String { print ("name: " + name) }