【发布时间】:2015-05-10 12:47:55
【问题描述】:
我正在尝试为 iPhone 制作一个应用程序,它应该使用用 nodejs + MongoDB 编写的 web 服务。该应用程序是用 Swift 制作的,但现在我遇到了一个问题,我没有正确地解析数据。
目前我有这样的代码:
var endpoint = NSURL(string: self.url + "?latitud=" + self.latitude + "&longitud=" + self.longitude)
var data = NSData(contentsOfURL: endpoint!)
var error: NSError? = nil
if let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary {
for place in json {
var name = place["obj"]["name"]
var coords = place["obj"]["coords"]
var annotation = MKPointAnnotation()
annotation.title = name as? String
annotation.coordinate = coords as? CLLocationCoordinate2D
map.addAnnotation(annotation)
}
}
不幸的是它不起作用:(
来自网络服务的响应与此类似:
[
{
"dis": 1.22,
"obj": {
"name": "Some name",
"coords": [
-97.1228,
17.4049
],
"phones": [
"555 555 55555",
"444 444 44444"
],
"address": {
"street": "Some Street",
"zip": "00000"
}
}
},
{
"dis": 2.03,
"obj": {
"name": "Othe name",
"coords": [
-97.0910
17.7099
],
"phones": [
"777 777 7777"
],
"address": {
"street": "Other street",
"zip": "11111"
}
}
}
]
这就是我做错了吗? 是否有更优雅(且特别高效)的方式向 API RESTful 发出请求?
【问题讨论】:
标签: ios json node.js rest swift