ios - 从 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,这是我看到的几乎每一篇关于这个的帖子都说要使用的。
解决方案
您可以访问该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)
}
}
推荐阅读
- javascript - 无法在渲染器进程中使用 Node.js API
- google-apps-script - 防止 CalendarApp 创建重复项
- javascript - “使用严格”是什么意思;globalThis.__codeBoxId = "gc8oqei2f4"; 在js中?
- sql - SQL - 每日查询的 Min()
- swift - Xcode Swift MacOS App,禁止在 NSScrollView 上输入文本
- http - 在 IntelliJ 集成 http 客户端中使用脚本形成请求正文
- java - 如何在 Java 文件中使用翻译编辑器
- unity3d - Unity 3d Text(更改 3d 文本)
- kotlin - 如何正确配置 HikariCP 以使用必须保持活动的连接?
- java - 参数化类 Java 的原始使用(使用泛型参数类型实现接口)