首页 > 解决方案 > 发布获取 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)中获取更多值。

提前致谢。

标签: swiftalamofireswift4

解决方案


我建议使用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"])

                   }

             }
        }
    }
}

推荐阅读