swift - 发布获取 JSON 值 SWIFT Alamofire
问题描述
我正在从服务器获得响应并将其转换为 NSDictionary。
Alamofire.request(URL, method: .post, parameters: parameters).responseJSON
{
response in
if let result = response.result.value {
let jsonData = result as! NSDictionary
let recordJSON=jsonData.value(forKey: "records") as! NSDictionary
let result = recordJSON.value(forKey: "result") as! NSArray
print(result)
}
}
现在结果值是
(
{
imagePath = "/SERVER/api/upload/geRsB.jpeg";
propertyId = 11;
userId = 5;
},
{
imagePath = "/SERVER/api/upload/RebJC.jpeg";
propertyId = 14;
userId = 5;
},
{
imagePath = "/SERVER/api/upload/fuM3F.jpeg";
propertyId = 18;
userId = 5;
}
)
那么,现在我如何从结果的每个索引(imagePath,propertyId,userId)中获取更多值。
提前致谢。
解决方案
我建议使用Decodable
,但在你的情况下,你可以试试
if let result = response.result.value as? [String:Any] {
if let recordJSON = result["records"] as? String:Any] {
if let result = recordJSON["result"] as? [Any] {
print(result)
for item in result {
if let inner = item as? [String:Any] {
print(inner["imagePath"])
print(inner["propertyId"])
print(inner["userId"])
}
}
}
}
}
推荐阅读
- dompdf - Dompdf 中的错误。它显示“class Dompdf\Dompdf”有致命错误并且不显示输出
- java - 使用 Retrofit 从服务器获取信息并将所需数据分配给 Spinner
- android - 为什么我无法登录谷歌玩游戏
- sublimetext3 - SublimeText:复制变量导入声明的插件?
- python-3.x - 如何使用 KMEANS 计算每个记录的聚类距离?
- python - 不关闭与 sqlite 数据库的连接有多安全?
- appium - 如何使用 Appium 将键发送到 Ionic 4 输入?
- python - 在数据框中,根据过滤条件将选定的单元格复制到同一数据框中的另一行
- selenium - 在 selenium 的不同选项卡中下载 PDF 文件以 URL 形式打开
- ios - 如何在 react-native 中调试`native Objective C code`?