2015-10-14 68 views
1

我有来自网站的JSON数据。我做了主要的字典,我可以解析除了一个子字典以外的所有数据。我得到错误“Swift:无法将类型'__NSCFArray'的值转换为'NSDictionary'”Swift:无法将'__NSCFArray'类型的值转换为'NSDictionary'

这是我的数据示例。我无法解析“天气”,但我可以解析所有其他字典,如"wind"

["name": Mountain View, "id": 5375480, "weather": (
     { 
     description = "sky is clear"; 
     icon = 01n; 
     id = 800; 
     main = Clear; 
    } 
), "base": cmc stations, "wind": { 
    deg = "129.502"; 
    speed = "1.41"; 

的片段代表您的评论代码

let windDictionary = mainDictionary["wind"] as! [String : AnyObject 
let speed = windDictionary["speed"] as! Double 
print(speed) 
let weather = mainDictionary["weather"] as! [String : AnyObject] 
print(weather) 
+0

看来我t是一个数组,而不是字典。尝试 let weather = mainDictionary [“weather”] as? [AnyObject] – dirtydanee

+1

weather is not dictionary ....'let weather = mainDictionary [“weather”] as! [[String:AnyObject]]' –

+0

但是相同语法的风词典 – Alexander

回答

13

...我想说windDictionary是字典...

Dictionary denotes in JSON with {} and 
Array denotes with [] // In printed response you may have array with() 

所以,你的露天部分是字典的数组。 ..你必须像解析它

let weather = mainDictionary["weather"] as! [[String : AnyObject]] // although please not use force unwrap .. either use `if let` or `guard` statement 
+0

不,没关系,现在好多了。感谢您的编辑。 :) – Moritz

相关问题