从AF.Request响应获取数据

人气:321 发布:2022-10-16 标签: ios swift alamofire

问题描述

我需要来自使用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"。但我尝试了许多不同的方法,但我无法从回复中获取所需的信息。另外,我没有响应.result t.value,它几乎是我看到的关于这篇文章的每个帖子都说要使用的。

推荐答案

您可以访问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)
            }
    }

223