简体   繁体   中英

Unable to get key value pair in Json response in swift 3?

I am having Json server response data like shown below in this I got all key value pairs using model class and unable to get only one key value pair street Can anyone help me how to get the street key value pair ?

here is my Json data

{
  "address": {
    "city": "chennai",
    "firstname": "sommesh",
    "lastname": "s",
    "email": "test@gmail.com",
    "postcode": "43",
    "street": [
      "[No: 14; 8th cross street]"
    ],
    "country_id": "US",
    "region_code": "CA",
    "region_id": "12",
    "telephone": "8756467654",
    "sameAsBilling": 1,
    "region": "California",
    "prefix": "",
    "company": "Test Company"
  }
}

Here is my model class code

struct GuestAddress {

        let id : Int
        let region : String
        let regionId : Any
        let regionCode : String
        let countryId : String
        let company : String
        let telephone : Any
        let postCode : Any
        let city : String
        let firstName : String
        let lastName : String
        let email : String
        let sameAsBilling : Any
        let saveInAddressBook : Any
        var street : [String]

        init(dict : [String:Any]) {
            self.id = dict["id"] as! Int
            self.region = dict["region"]! as! String
            self.regionId = dict["region_id"]!
            self.regionCode = dict["region_code"]! as! String
            self.countryId = dict["country_id"]! as! String
            self.company = dict["company"]! as! String
            self.telephone = dict["telephone"]! as! String
            self.postCode = dict["postcode"]!
            self.city = dict["city"]! as! String
            self.firstName = dict["firstname"]! as! String
            self.lastName = dict["lastname"]! as! String
            self.email = dict["email"]! as! String
            self.sameAsBilling = dict["same_as_billing"]!
            self.saveInAddressBook = dict["save_in_address_book"]!
            let guestStreet = dict["street"] as! [String]
            var streetArr = street
            for item in guestStreet  {
                streetArr.append(item)
            }
            street = streetArr
        }
      }

Here is my postman data image

Initialise the variable first

var street : [String] = [ ]

Because Here, You have declared

var street : [String]

which is never initialised, There after you have written

var streetArr = street

That means "streetArr" also not initialised. So

streetArr.append(item) does nothing.

you have redundant code work.

Correct it as following:

 var street : [String] = []

init(dict : [String:Any]) {

     :
     :
     :
     :
     let guestStreet : [String] = dict["street"] as! [String] //2. here, guestStreet is the array of address.

 // 3. Print guestStreet
 //[ "[No: 14; 8th cross street]" ]

 // get required result

for item in guestStreet  {

    var address = item
    address.remove(at: address.startIndex)  //4. remove first brace
    address = String(address.characters.dropLast()) //5. remove last brace
    street.append(address) //6. Append address

}

print(street.first!) //7.  Output

Thats it.

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM