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"
}
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
George Heints
  • 1,303
  • 3
  • 20
  • 37
  • 3
    Make your class implement Codable and then read up on how to implement that protocol properly. Also, no need to subclass NSObject – Joakim Danielson Jul 13 '18 at 06:48
  • 2
    Are all the fields really optional `var`s? It's make your life easier if fields that must be present use non-optional `let`… e.g. `let id: String` – Ashley Mills Jul 13 '18 at 07:14
  • Possible duplicate of [Swift 4 - JSON parsing with Codable protocol (nested data)](https://stackoverflow.com/questions/46935354/swift-4-json-parsing-with-codable-protocol-nested-data) – serg_zhd Jul 13 '18 at 10:01

2 Answers2

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.  
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Sagar Chauhan
  • 5,715
  • 2
  • 22
  • 56
  • i get this error now... "Cannot convert value of type 'JSON' to expected argument type 'Data'" i got JSON from FB auth.. – George Heints Jul 13 '18 at 08:07
  • @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. – Sagar Chauhan Jul 13 '18 at 09:29
0

I will suggest url for creating model.

  1. http://www.jsoncafe.com

  2. https://app.quicktype.io

here you put your json then convert.

Chandan Jee
  • 5,440
  • 4
  • 16
  • 24