2
我正在提出一個簡單的請求來從JSON文件中獲取特定的數據,但我在獲取確切數據時遇到問題。使用Alamofire獲取數組的關鍵值
的JSON文件:
{
"Something1": {
"Added": "09-10-2016",
"Expires": "09-12-2016",
"Reliability": "78%",
"Views": "2",
"Priority": "High"
},
"Something2": {
"Added": "09-11-2016",
"Expires": "09-13-2016",
"Reliability": "98%",
"Views": "5",
"Priority": "Low"
}
}
的SWIFT代碼:
Alamofire.request("https://example.com/args.json").responseJSON { response in
if let JSON = response.result.value as? [String:AnyObject] {
print(JSON["Something1"])
}
}
隨着print(JSON["Something1"])
,它打印家居Something1就像它應該,但是當我嘗試做print(JSON["Something1"]["Views"])
拋出一個錯誤例如。我將如何去解決這個問題?
我想你必須把你的JSON [「Something1」]作爲[String:AnyObject] – koropok
@koropok這絕對適合我!謝謝! – Matt
不客氣! – koropok