這裏我想通過url解析JSON。這是在url上可用的實際JSON數據。所以我需要解析它並使用Alamofire在我的應用程序中閱讀。但我無法做到。如何使用SWIFT3解析此JSON
JSON數據在我的網址。
[{"date":"2017-01-15","USDARS":"15.839023","USDCLP":"659.200012","USDVEF":"9.974801","USDCOP":"2943","USDPEN":"3.359497","USDBOB":"6.859692","USDPYG":"5780.799805","USDUYU":"28.660097","USDGTQ":"7.533499","USDCUP":"0.999845","USDCRC":"547.599976","USDHTG":"64.129997","USDJMD":"128.309998","USDDOP":"46.230097","USDETB":"22.459999","USDNGN":"314.999972","USDEGP":"18.900947","USDCDF":"1227.999527","USDTZS":"2216.999869","USDKES":"103.650002","USDSDG":"6.4077"},
{"date":"2017-01-16","USDARS":"15.849011","USDCLP":"662.099976","USDVEF":"9.974797","USDCOP":"2936.800049","USDPEN":"3.367975","USDBOB":"6.860284","USDPYG":"5733.700195","USDUYU":"28.749899","USDGTQ":"7.545501","USDCUP":"0.999646","USDCRC":"547.099976","USDHTG":"64.129997","USDJMD":"128.179993","USDDOP":"46.230123","USDETB":"22.39805","USDNGN":"313.999947","USDEGP":"18.898647","USDCDF":"1225.999725","USDTZS":"2221.999959","USDKES":"103.650002","USDSDG":"6.407702"},
{"date":"2017-01-17","USDARS":"15.882954","USDCLP":"656.299988","USDVEF":"9.974798","USDCOP":"2914","USDPEN":"3.3392","USDBOB":"6.860283","USDPYG":"5771.000017","USDUYU":"28.680621","USDGTQ":"7.533498","USDCUP":"0.999979","USDCRC":"547.080017","USDHTG":"64.120003","USDJMD":"128.580002","USDDOP":"46.200001","USDETB":"22.397891","USDNGN":"314.999989","USDEGP":"18.649708","USDCDF":"1225.000306","USDTZS":"2227.000108","USDKES":"103.750025","USDSDG":"6.407696"}]
我怎麼了在SWIFT 3.0
下面使用Alamofire是實際我試圖通過URL解析上述JSON數據讀取。
Alamofire.request("myurl") .responseJSON { response in
print("In Alamofire")
if let arr = response.result.value as? [String:AnyObject]
{
if let arr = response.result.value as? [NSDictionary]
{
let val1 = (arr["main"]["USDARS"] as? String)
print(val1)
//It does not print any thing.
}
}
}
請幫幫我。我是新手。
爲什麼有兩個'if let arr'?不要使用'NSDictionary',使用Swift Types。它通過了第一次測試嗎?如果是,如果let main = arr [「main」]爲? [[String:AnyObject]]'(字典數組)應該是下一個邏輯步驟。然後'讓val1 = main [0] [「USDARS」]爲? String'? – Larme
你好,它不起作用 – aadityaa
什麼是行不通的?你是一個潔具,你正在做多個「如果」測試,但是他們中的任何一個都可能失敗?哪一個確切(你可以添加一個「其他」案例)?你甚至通過了第一個? – Larme