【发布时间】:2015-07-08 08:28:30
【问题描述】:
我一直在尝试将 JSON 字符串转换为字典值,但我无法让它工作,我得到一个空值。在我尝试提取数据之前,我检查了我是否得到了所有 JSON,所以很明显我在这里做错了
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options:
NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
var items = [[String:String]()]
var item:AnyObject
var authorDictionary:AnyObject
for var i = 0; i < jsonResult["item"]?.count; i++ {
items.append([String:String]())
item = (jsonResult["items"] as? [[NSObject:AnyObject]])!
items[i]["content"] = item["content"] as? String
items[i]["title"] = item["title"] as? String
items[i]["publishedDate"] = item["published"] as? String
authorDictionary = item["author"] as! NSDictionary
items[i]["author"] = item["displayName"] as? String
}
println(items)
这就是我得到的结果: [[:]]
我是 JSON 新手,谁能解释我应该做什么以及我做错了什么?
【问题讨论】:
-
你能展示你的 JSON 吗?我的猜测是根对象不是包含
item数组的字典
标签: ios json swift dictionary