【问题标题】:Parse Alamofire json response解析 Alamofire json 响应
【发布时间】:2017-04-18 05:15:19
【问题描述】:

我正在尝试解析来自 Alamofire 的响应,但我不知道该怎么做。

这是我得到的 JSON 响应(我想解析出“结果”)这是怎么做到的?

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

斯威夫特 3

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

【问题讨论】:

  • 请检查更新的答案

标签: json swift swift3 alamofire


【解决方案1】:

您只需指定响应的类型为Dictionary,然后使用subscript 和字典来获取result 的值。

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

    let result = dictionary["result"] ?? 0
    print(result)
}

【讨论】:

    【解决方案2】:
    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)")
    }
    

    【讨论】:

      【解决方案3】:

      根据最新的 Almofire LibSwift 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)
       }
      

      【讨论】:

      • 停止比较 java 和 ios :-)。我们肯定会在 10 年后讨论它,因为我们都知道哪种语言有未来。顺便说一句,我是 Java 开发人员,而且很有趣 :-)
      • 在快速的世界里感觉像外星人。希望随着时间的推移会变得更好 :-) 再次感谢您的意见
      猜你喜欢
      • 2021-07-22
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      • 2023-02-01
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      相关资源
      最近更新 更多