2017-08-03 71 views
0

我從http請求中獲取JSON響應。得到它後,它有數組並嘗試解析。Swift:從JSON請求中解析數組元素

SWIFT代碼:

if let finalResponse = parseJSON["visuals"] as? [String: Any] { 


       let balanceResponse = finalResponse ["balanceList"] as? AnyObject 
       print("balanceResponse:: \(balanceResponse)") 

       let dateResponse = finalResponse ["dateList"] as? AnyObject 
       print("dateResponse:: \(dateResponse)") 

打印日誌:

balanceResponse:: Optional(<__NSArrayM 0x17404b880>(
{ 
    data =  (
     "32872.23", 
     "38814.87", 
     "38915.85" 
    ); 

} 
dateResponse:: Optional(<__NSArrayM 0x17005a4f0>(
Apr 26, 2017, 
Jun 10, 2017, 
Jul 26, 2017 
) 

如何訪問在數據 3個值從陣列和3日期值dateResponse

+0

你使用像SwiftyJson庫? –

+0

不,我不使用, – Stella

回答

0

dateResponsebalanceResponse將返回一個新的數組。因此,只要將它們視爲任何其他數組並使用索引訪問該元素並填充模型即可。

其他簡單的方法我會推薦你​​使用像ObjectMapper或SwiftyJson這樣的第三方庫它會減少你的工作,它們是非常穩定的解決方案。

UPDATE:

let balanceResponse = finalResponse ["balanceList"] as? AnyObject 
let data = balanceResponse[「data」] 
let firstValue = data[0] 
+0

我可以像使用索引的任何其他標準數組一樣訪問第二個數組(dateResponse),但第一個(balanceResponse)無法像使用索引的標準數組那樣訪問。 – Stella

+0

它是數組內的數組。檢查我的更新 – Joe

+0

它不起作用。 – Stella