ios - NSJSONSerialization 使用有效的 json 返回 nil

标签 ios json swift

我尝试解析返回带有 NSJSonSerialization 的有效 JSON 的响应。但它返回 nil 没有错误。它适用于另一个 JSON 响应。

我查了一下,可能是JSON编码的问题。我不知道如何解决它。任何想法 ?

let url: NSURL = NSURL(string: urlPath)!
        self.searchRequest = NSMutableURLRequest(URL: url)
        if let searchRequest = self.searchRequest {
            searchRequest.HTTPMethod = "GET"
            let authString : String = SNCF.APIKey + ":" + ""
            let authData : NSData = authString.dataUsingEncoding(NSASCIIStringEncoding)!
            let authValue : String = "Basic " + authData.base64EncodedStringWithOptions(.EncodingEndLineWithCarriageReturn)
            searchRequest.setValue(authValue, forHTTPHeaderField: "Authorization")

            let queue:NSOperationQueue = NSOperationQueue()
            NSURLConnection.sendAsynchronousRequest(searchRequest, queue: queue, completionHandler:{ (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in

            do {
//HERE JSONRESULT WILL BE NIL
                if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: [AnyObject]] {
                    print("ASynchronous\(jsonResult)")
                    if let places = jsonResult["stop_areas"] as? [[String:AnyObject]]{
                        for placeDictionary in places {
                            if let labelText = placeDictionary["label"] as? String {
                                self.resultDatasource.append(labelText)
                            }
                        }
                        self.resultTableView.reloadData()
                    }
                }
//HERE NO ERROR IS CATCHED
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        })

我的 json 响应片段:

{
  "disruptions": [],
  "pagination": {
    "start_page": 0,
    "items_on_page": 100,
    "items_per_page": 100,
    "total_result": 3053
  },
  "stop_areas": [
    {
      "codes": [
        {
          "type": "CR-CI-CH",
          "value": "0080-251967-BV"
        }
      ],
      "name": "gare de Perl",
      "links": [],
      "coord": {
        "lat": "0",
        "lon": "0"
      },
      "label": "gare de Perl",
      "timezone": "Europe/Paris",
      "id": "stop_area:OCE:SA:80251967"
    },
    {
    ...
    },
    //stop_areas dictionaries object...
], //end stop_areas array of dictionaries
"links": [
    {
      "href": "https://api.sncf.com/v1/coverage/sncf/stop_areas/{stop_areas.id}",
      "type": "stop_areas",
      "rel": "stop_areas",
      "templated": true
    },
    {
      "href": "https://api.sncf.com/v1/coverage/sncf/stop_areas?start_page=1",
      "type": "next",
      "templated": false
    },
    {
      "href": "https://api.sncf.com/v1/coverage/sncf/stop_areas?start_page=30.52",
      "type": "last",
      "templated": false
    },
    {
      "href": "https://api.sncf.com/v1/coverage/sncf/stop_areas",
      "type": "first",
      "templated": false
    }
  ],
  "feed_publishers": [
    {
      "url": "",
      "id": "sncf",
      "license": "",
      "name": ""
    }
  ]
}

最佳答案

JSON 的类型是 [String: AnyObject] 而不是 [String: [AnyObject]]

关于ios - NSJSONSerialization 使用有效的 json 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36999191/

相关文章:

ios - 无法将数据从 Collection View 传递到 Xcode 中的详细信息 View

ios - 嵌入另一个 View 时,SwiftUI 动画不起作用

ios - UITableViewDelegate 无法工作并出现错误( fatal error : Unexpectedly found nil while unwrapping an Optional value in UITableView in swift 4)

ios - Swift iOS 9.2 中的每日本地通知

ios - 如何使用字典数组 iOS swift 解析 JSON

ios - 启用后禁用 allowsBackgroundLocationUpdates (CLLocationManager) 不起作用

ios - 网络扩展授权,如何启用?

Android从mysql服务器获取包含特殊/复制粘贴字符的数据

php - 通过 url 传递 json 数组在 IE 中不起作用

json - 在 Laravel 5.4 中禁用请求验证重定向