首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >具有有效json的NSJSONSerialization返回零

具有有效json的NSJSONSerialization返回零
EN

Stack Overflow用户
提问于 2016-05-03 08:27:27
回答 3查看 754关注 0票数 0

我试图解析一个返回带有NSJSonSerialization的有效JSON的响应。但它没有错误地返回零。它与另一个JSON响应一起工作。

我做了一些搜索,这可能是JSON编码的一个问题。我不知道怎么解决。知道吗?

代码语言:javascript
运行
复制
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回答是:

代码语言:javascript
运行
复制
{
  "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": ""
    }
  ]
}
EN

Stack Overflow用户

回答已采纳

发布于 2016-05-03 08:34:46

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

票数 5
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36999191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档