首页 > 解决方案 > 从 AF.Request 响应中获取数据

问题描述

我需要来自使用 Alamofire 的 Post 请求调用的 json 响应中的数据,但由于某种原因我无法访问该数据

我尝试跟随 Alamofire github 文档以及这篇文章get data from AF responseJSON。但都没有帮助我。

AF.request("https://mbd.cookcountysupernetwork.com/ap/swift_math_get.asp", method: .post,  parameters: parameters, encoding: JSONEncoding.default)
            .responseJSON { response in
                print(response)
                print("floop")

        }

这是我在代码运行时看到的

success({
    Operand =     (
                {
            A = 12;
        },
                {
            B = 25;
        }
    );
    Operation = Multiply;
    Result = 300;
})

所以我知道 json 在那里,我只需要访问“Result = 300”,这样我就可以设置一个文本框说“300”。但我尝试了许多不同的方法,但我无法从响应中访问我需要的信息。我也没有 response.result.value,这是我看到的几乎每一篇关于这个的帖子都说要使用的。

标签: iosswiftalamofire

解决方案


您可以访问该Result值,

AF.request("https://mbd.cookcountysupernetwork.com/ap/swift_math_get.asp", method: .post,  parameters: parameters, encoding: JSONEncoding.default)
        .responseJSON { response in
            switch response.result {
            case .success(let value):
                if let json = value as? [String: Any] {
                    print(json["Result"] as? Int)
                }
            case .failure(let error):
                print(error)
            }
    }

推荐阅读