2015-10-14 54 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

相關問題