【问题标题】:Parsing a json for geolocation iOS app解析用于地理定位 iOS 应用程序的 json
【发布时间】: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


    【解决方案1】:

    如果你替换,你的 rest API 返回一个 NSDictionary 数组

    if let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary {

    if let boardsDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as? Array<NSDictionary> {
    您应该将数据作为数组获取。这将让您访问数据

    【讨论】:

    • 最好用数组代替NSArray?
    • 这里stackoverflow.com/a/24038680/2976077 是关于本机类型的一个很好的答案。使用 swift 的原生类型是常见的做法,即本例中为数组。
    【解决方案2】:

    来自网络服务的响应数据是 JSONArray,您应该将其转换为 NSArray 而不是 NSDictionary。

        if let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary {
    

    我对 swfit 不熟悉,但看来您应该更改此行。

    如果您对来自客户端的 RESTful API 调用有任何问题,您可以试试这个http://unirest.io/

    Unirest 是一个相当不错的轻量级 HTTP 客户端,用于进行 RESTful API 调用和使用 JSON 解析响应。

    【讨论】:

    • 您说的很对,为我更改 NSArray NSDictionary 更好。 Unirest 很棒,感谢您的推荐
    【解决方案3】:

    我不了解 Swift,但我认为您忘记了代码中的引号......

    var coords = place["obj"]["coords]

    也许:

    var coords = place["obj"]["coords"]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      相关资源
      最近更新 更多