相关疑难解决方法(0)

在Swift,AnyObject类型中解析json

我正在尝试解析json,但我对数据类型有一些困难,特别是AnyObject类型+向下转换.

让我们考虑下面的json(它是一个完整的json的摘录).

{  "weather":
   [
      {
         "id":804,
         "main":"Clouds",
         "description":"overcast clouds",
         "icon":"04d"
      }
   ],
}
Run Code Online (Sandbox Code Playgroud)

对我来说,json可以描述如下:

- json: Dictionary of type [String: AnyObject] (or NSDictionary, so = [NSObject, AnyObject] in Xcode 6 b3)
    - "weather": Array of type [AnyObject] (or NSArray)
         - Dictionary of type [String: AnyObject] (or NSDictionary, so = [NSObject, AnyObject] in Xcode 6 b3)
Run Code Online (Sandbox Code Playgroud)

我的json是AnyObject类型!(我用来JSONObjectWithData从URL获取JSON).

然后我想访问天气词典.这是我写的代码.

var localError: NSError?
var json: AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &localError)

if let dict = json as? [String: AnyObject] …
Run Code Online (Sandbox Code Playgroud)

parsing json swift

39
推荐指数
2
解决办法
5万
查看次数

标签 统计

json ×1

parsing ×1

swift ×1