3

I want to store the fetched JSON data to my model class array propertyArray. Some how I got the JSON data using Alamofire library and now I want to parse that data to the properties of PropertyList model class. I am not able to parse JSON data to propertyArray. I referred many other solutions to this question on stack overflow but did not get any appropriate solution.

Declared Model Class Array

var propertyArray: [PropertyList]? = []

Alamofire Function

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)
        }
    }
}

PropertyList

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 Data

{
  "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"
    }
  ]
}
7
  • What actually happens when you run the code you have so far? What further help do you need? There are countless examples and tutorials about this. Commented Jan 11, 2018 at 6:05
  • I want to see my JSON data into my array but I don't know how to do that within alamofire. Commented Jan 11, 2018 at 6:15
  • Yes, that's clear. Please read my first comment again. Clearly explain what your actual issue is with the code you posted. Commented Jan 11, 2018 at 6:17
  • How can i access the JSON tree objects? If I am able to access the tree, I want to assign them to the model class properties Commented Jan 11, 2018 at 6:22
  • @rmaddy Sir, I have recently started programming in swift. So I need some basic guidance from my friends here regarding the same. Commented Jan 11, 2018 at 6:31

1 Answer 1

1

Looks like your JSON is an array that contains dictionaries. (Key-Value data structure)

This should do the trick.

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

Then parse it inside your constructor:

struct PropertyList{

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

}

To add to array:

 for dict in data{
      let propertyList = PropertyList(dict: dict)
      propertyArray.append(propertyList)
 }
Sign up to request clarification or add additional context in comments.

9 Comments

Yes. My array is a key-value data. I will try this out and let you know.
Yes. See my app processing goes like - Firstly, I need to JSON data into my code so I chose Alamofire for fetching JSON data from URL. Then, I want to assign the JSON data to the model class array propertyArray. Later, I will use the array to populate my table view
I literally gave you the answer from the top. You have the dictionary, now you just need to assign it your properties...
Exactly. I don't know how to assign them to properties
Sorry. I did not checked your answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.