2017-04-18 124 views
0

我想解析來自Alamofire的響應,但我無法弄清楚如何去做。解析Alamofire json響應

這是我得到的JSON響應(我想解析出「結果」)這是如何完成的?

JSON: { 
    result = 887957; 
    status = 0; 
} 

斯威夫特3

if let JSON = response.result.value { 
print("JSON: \(JSON)") 
} 
+0

請爲什麼downvote檢查更新的答案 –

回答

2

你只需要指定響應的類型是Dictionary然後用subscript用字典來獲得的result值。

if let dictionary = response.result.value as? [String: Int] { 

    let result = dictionary["result"] ?? 0 
    print(result) 
} 
0
if let JSON = response.result.value as? [String : Any] { 
    let result = JSON["result"] as? Int 
    let status = JSON["status"] as? Int 
    print("Result \(result) Status \(status)") 
} 
-1

按照最新Almofire庫雨燕3.0採用適當的驗證:

case .success(_): 
if ((response.result.value) != nil) { 
    var responseData = JSON(response.result.value!) 

    //Userdefaults helps to store session data locally just like sharedpreference in android 
    if (response.response ? .statusCode == 200) { 
    let result: Int = responseData["result"].int! 
    let status: Int = responseData["status"].int! 

    } 
} 


case .failure(_): 
print(response.result) 
} 
+0

??? ...是的,你可以使用編輯答案選項tooo .... –

+0

停止比較Java和ios :-)。我們肯定會在10年後討論它,因爲我們都知道哪種語言有未來.Btw我是Java開發人員並且很快樂:-) –

+0

感覺像在swift世界中的外星人。希望隨着時間的過去會變得更好:-)感謝您輸入一次再次 –