-1
我做一個GET請求,Blockchain API,這裏是我所得到的:無法投型「__NSCFDictionary」價值「的NSArray
{
"USD": {
"15m": 1973.51,
"last": 1973.51,
"buy": 1973.51,
"sell": 1970.5,
"symbol": "$"
},
"ISK": {
"15m": 204455.64,
"last": 204455.64,
"buy": 204455.64,
"sell": 204143.8,
"symbol": "kr"
},
"HKD": {
"15m": 15400.36,
"last": 15400.36,
"buy": 15400.36,
"sell": 15376.88,
"symbol": "$"
},
"TWD": {
"15m": 59959.18,
"last": 59959.18,
"buy": 59959.18,
"sell": 59867.73,
"symbol": "NT$"
}
}
我嘗試分析USD子對象,而這是我的代碼:
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error);
} else {
let responseData = String(data: data!, encoding: String.Encoding.utf8);
print(responseData);
var USD = [String]()
do{
if let ipString = NSString(data:data!, encoding: String.Encoding.utf8.rawValue) {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [NSDictionary];
USD = json.map { ($0 as! [String: AnyObject])["USD"] as! String }
}
}
catch{
print("Parse JSON failed")
}
}
})
但是當我運行它,有一個錯誤:
Could not cast value of type '__NSDictionaryM' to 'NSArray'
我知道USD
更像是[String:AnyObject]
,但它也沒有工作。任何解決方案
你爲什麼使用'NSString'和'NSDictionary'? – rmaddy