ios - 将 JSON 数据存储到 Swift 4 中的模型类数组中

标签 ios swift alamofire

我想将获取的 JSON 数据存储到我的模型类数组 propertyArray。一些我如何使用 Alamofire 库获取 JSON 数据,现在我想将该数据解析为 PropertyList 模型类的属性。我无法将 JSON 数据解析为 propertyArray。我在堆栈溢出上提到了这个问题的许多其他解决方案,但没有得到任何合适的解决方案。

声明的模型类数组

var propertyArray: [PropertyList]? = []

Alamofire 函数

func dataPass()  {
    print("Landlord id is \(land.id)")
    let  para: Parameters = ["id": land.id]
    Alamofire.request(URL_Landlord_Property_List, method: .post, parameters: para).responseJSON { response in
        if let dictionary = response.result.value as? [String : AnyObject]{
            var prop = PropertyList()
            let data = dictionary["data"] as! [Any]
            print(data)
        }
    }
}

属性列表

import Foundation

struct PropertyList{
    var property_id: String?
    var user_id: String?
    var property_code: String?
    var property_added_date: String?
    var property_updated_date: String?
    var property_termination_date: String?
    var property_ASYS_no: String?
    var propertyCode: String?
    var property_address_1:String?
    var property_address_2: String?
    var property_address_3: String?
    var property_city: String?
    var property_cluster:String?
    var property_area:String?
    var property_postcode: String?
    var property_landlord_ref_code: String?
    var property_landlord_id: String?
}

JSON 数据

{
  "success": true,
  "data": [
    {
      "property_id": "1",
      "user_id": "1",
      "property_code": "0001",
      "property_added_date": "2017-12-13",
      "property_updated_date": "2017-12-13 00:00:00",
      "property_termination_date": null,
      "property_ASYS_no": "ASYS 001",
      "propertyCode": "0001-PUNE1",
      "property_address_1": "PUNE1",
      "property_address_2": "PUNE2",
      "property_address_3": "PUNE3",
      "property_city": "PUNE",
      "property_cluster": "1",
      "property_area": "1",
      "property_postcode": "424031",
      "property_landlord_ref_code": null,
      "property_landlord_id": "1"
    },
    {
      "property_id": "2",
      "user_id": "1",
      "property_code": "0002",
      "property_added_date": "2017-12-14",
      "property_updated_date": "2017-12-18 00:00:00",
      "property_termination_date": null,
      "property_ASYS_no": "asys 0200",
      "propertyCode": "0002-hadpasar1",
      "property_address_1": "hadpasar1",
      "property_address_2": "hadpasar2",
      "property_address_3": "hadpasar3",
      "property_city": "pune",
      "property_cluster": "9",
      "property_area": "2",
      "property_postcode": "012121",
      "property_landlord_ref_code": null,
      "property_landlord_id": "1"
    }
  ]
}

最佳答案

看起来您的 JSON 是一个包含字典的数组。 (键值数据结构)

这应该可以解决问题。

 let data = dictionary["data"] as! [[String: Any]]

然后在你的构造函数中解析它:

struct PropertyList{

    init(dict: [String: Any){
     //parse here...
       self.property_id = dict["property_id"] as! String
    }

}

添加到数组:

 for dict in data{
      let propertyList = PropertyList(dict: dict)
      propertyArray.append(propertyList)
 }

关于ios - 将 JSON 数据存储到 Swift 4 中的模型类数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48200682/

相关文章:

swift - 使类最终化有助于编译时间更快吗?

ios - 为什么 iOS Swift 无法更改 UITabBar 角标(Badge)字体?

使用 Alamofire 从 iOS 上传 PHP

ios - 如何更新另一个 UIViewController 上的进度 View - SWIFT?

ios - 退出 'while' 循环后数组项被删除?

iphone - 选择另一个日历时 EKEvent 标识符发生变化

ios - 我可以在 Swift 5 和 IOS 12 中以编程方式更改 iOS 屏幕壁纸吗

ios - iTunes Store 作为模态视图

ios - 在自定义对象数组中查找项目并更改值 - Swift

ios - Alamofire 上传巨大的文件