GoSlowPoke168
GoSlowPoke168

Reputation: 35

How do I decode this JSON Data in Swift?

How do I decode this JSON Data?

I've done it with the "drilling down" method where I kept calling each key and printing the value. I also tried the data model but it never worked. I probably did something wrong but I don't know what.

Thanks in advance, If you need more information, just ask, I'm fairly new to Swift and Stackoverflow.

   {
       "items":[
          {
             "id":16000014,
             "name":"BO",
             "starPowers":[
                {
                   "id":23000090,
                   "name":"CIRCLING EAGLE"
                },
                {
                   "id":23000148,
                   "name":"SNARE A BEAR"
                }
             ],
             "gadgets":[
                {
                   "id":23000263,
                   "name":"SUPER TOTEM"
                },
                {
                   "id":23000289,
                   "name":"TRIPWIRE"
                }
             ]
          },
          {
             "id":16000015,
             "name":"PIPER",
             "starPowers":[
                {
                   "id":23000091,
                   "name":"AMBUSH"
                },
                {
                   "id":23000152,
                   "name":"SNAPPY SNIPING"
                }
             ],
             "gadgets":[
                {
                   "id":23000268,
                   "name":"AUTO AIMER"
                },
                {
                   "id":23000291,
                   "name":"HOMEMADE RECIPE"
                }
             ]
          }
       ],
       "paging":{
          "cursors":{
    
          }
       }
    }

Upvotes: 0

Views: 63

Answers (2)

Sarthak Mishra
Sarthak Mishra

Reputation: 1114

Decoding JSON in swift is insanely easy. Just use the JSONDecoder class. Firstly, create a Codable class for your json response like this

struct Items: Codable {
    let items: [Item]
    let paging: Paging
}

struct Item: Codable {
    let id: Int
    let name: String
    let starPowers, gadgets: [Gadget]
}

struct Gadget: Codable {
    let id: Int
    let name: String
}

struct Paging: Codable {
    let cursors: Cursors
}

struct Cursors: Codable {
}
 

And then use it to parse your JSON like this

 let decoder = JSONDecoder()

do {
    let items = try decoder.decode(Items.self, from: jsonData)
    print(items)
    // Do something with the items here

} catch {
    print(error.localizedDescription)
}

Upvotes: 1

struct Name {
    var id:Int
    var name:String
}

struct Cursor {
    // Your cursor model
}

struct Paging {
    var cursors: Cursor
}

struct Items {
    var id:Int
    var name:String
    var starPowers:[Name]
    var gadgets:[Name]
}

struct MainModel {
     var items : [Items]
     var paging : Paging
}

You can decode that data using let yourData = try! JSONDecoder().decode(MainModel.self, from: jsonData) to get your desired JSON data.

Upvotes: 0

Related Questions