3

I want to put all data from my JSON to model class. How i can do that? All fields and my code i will put right below!

Model

class FacebookUser: NSObject {
    var first_name: String?
    var id: String?
    var last_name: String?
    var name: String?
    var picture: String?

    init(dictionary: [String: AnyObject]) {
        self.first_name = dictionary["first_name"] as? String
        self.id = dictionary["id"] as? String
        self.last_name = dictionary["last_name"] as? String
        self.name = dictionary["name"] as? String
        self.picture = dictionary["picture"] as? String
    }
}

JSON EXAMPLE

{
    "picture" : {
        "data" : {
            "height" : 50,
            "is_silhouette" : false,
            "url" : "link",
            "width" : 50
        }
    },
    "name" : "George Heinz",
    "last_name" : "Heinz",
    "id" : "1860499320637949",
    "first_name" : "George"
}
3
  • 3
    Make your class implement Codable and then read up on how to implement that protocol properly. Also, no need to subclass NSObject Commented Jul 13, 2018 at 6:48
  • 2
    Are all the fields really optional vars? It's make your life easier if fields that must be present use non-optional let… e.g. let id: String Commented Jul 13, 2018 at 7:14
  • Possible duplicate of Swift 4 - JSON parsing with Codable protocol (nested data) Commented Jul 13, 2018 at 10:01

2 Answers 2

2

I assume that you have json in Data format and All field Optional

Create following decodable json model class which are used to decode your json data.

struct PictureJson: Decodable {
    var picture         : Data?
    var name            : String?
    var last_name       : String?
    var id              : String?
    var first_name      : String?
}

struct Data: Decodable {
    var data            : ImageData?
}

struct ImageData : Decodable {
    var height          : Int?
    var is_silhouette   : Bool?
    var url             : String?
    var width           : Int?
}

And write following code to decode your json

do {
    let picture = try JSONDecoder().decode(PictureJson.self, from: jsonData!) as? PictureJson
     print(picture!.picture!.data)
        
} catch {
    // print error here.  
}
Sign up to request clarification or add additional context in comments.

2 Comments

i get this error now... "Cannot convert value of type 'JSON' to expected argument type 'Data'" i got JSON from FB auth..
@GeorgeHeints, Please check JSON's super class and make sure it should be data. If JSON is [String: Any] then your need to encode it to data form.
2

I will suggest url for creating model.

  1. http://www.jsoncafe.com

  2. https://app.quicktype.io

here you put your json then convert.

1 Comment

jsoncafe.com does not exist anymore

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.