0

I want to parse this JSON :http://jsonplaceholder.typicode.com/users I have a problem for find JSON structure. I am trying JSON with this structure that is work well, but I don't sure of this is better way or is not! what is the best way for parsing this JSON array to post instance? there is my code:

 func profileFromJSONData(data : NSData) -> ProfileResult {


         do{
        let jsonObject : NSArray!
  = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! NSArray

                for profileJSON in jsonObject {
                    if let profile = profileFromJsonObject(profileJSON as! NSDictionary) {



                        finalProfile.append(profile)
                    }
                }

                return .Success(finalProfile)
            }
            catch let error {
                return .Failure(error)
            }


        }



     func profileFromJsonObject(json: NSDictionary) -> UserProfile?{

            guard let
                id = json["id"] as? Int,
                name = json["name"] as? String,
                userName = json["username"] as? String,
                email = json["email"] as? String,
                address = json["address"] as? NSDictionary,
                phone = json["phone"] as? String,
                website = json["website"] as? String,
                company = json["company"] as? NSDictionary
                else {
                    return nil
            }
            let obj = UserProfile(id: id, name: name, userName: userName, email: email, address: address, phone: phone, website: website, company: company)

            return obj
        }

1 Answer 1

1

Here is what apple suggestion when Working with JSON in Swift,

and you can improve your code to one line by using flatMap

change from:

for profileJSON in jsonObject {
    if let profile = profileFromJsonObject(profileJSON) {
        finalProfile.append(profile)
    }
}

to:

finalProfile += jsonObject.flatMap(profileFromJsonObject)

That's the same.

Deal with address:

struct Address {
    var street: String
    var city: String
    ...
    init?(json: [String: AnyObject]){...}

}

extension Address: CustomStringConvertible {

    var description: String {

        return "street: \(street), city: \(city)"
    }
}

func profileFromJsonObject(json: [String: AnyObject]) -> UserProfile? {

    guard let
            ...
            addressJson = json["address"] as? [String: AnyObject]
            address = Address(json: addressJson),
            ...
            else {
                return nil
        }

    let obj = UserProfile(id: id, name: name, userName: userName, email: email, address: address, phone: phone, website: website, company: company)

    print(address) // output: "street: Kulas Light, city: Gwenborough"

}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your suggestion,so how can have a string of address detail?
Answer updated: you can add a property description to Address, And return a readable string in it.Then you can use the property when needed. This is a bit like a relational database. If you have use core data, you will find it acts like ` to one relationship`.

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.