简体   繁体   中英

SWIFT 4 nested JSON Struct - Codable

I'm having issues creating a struct to parse JSON in Swift 4. I'm able to parse small JSONs and JSONDecoder seems to work fine. Just need help to create a struct to parse JSON like that:

{
    "main": {
        "solutions": [
                    {
                    "exersises": [
                                     {
                                     "book_title": "test",
                                     "release_date": "2015-01-12T11:00",
                                     "price": 100,
                                     "additional": [
                                                   {
                                                   "item1": "test",
                                                   "item2": "test",
                                                   "number": 1
                                                   },
                                                   {
                                                    "item1": "test2",
                                                    "item2": "test2",
                                                    "number": 2
                                                   }
                                                   ],
                                     "availability": "Yes",
                                     "item_id": 43534
                                     }
                                     ]


                    }

                    ]
    }
}

What kind of struct do I need to get to value of book_title for example?

Its really easy. Your main probem is most likely root element. Let me get first layer or two for you.

let decoded = try JSONDecoder().decode(MainJSON.self, from: data)

class MainJSON: Codable {
    var main:SolutionJSON?
}

class SolutionJSON: Codable {
    var exercises:[ExercisesJSON]?
}

class ExercisesJSON: Codable {
    var bookTitle: String?
    var releaseDate: String?
    var price: Double?
    ... etc

    enum CodingKeys: String, CodingKey {
        case bookTitle = "book_title"
        case releaseDate = "release_date"
        case price = "price"
    }
}

ExerciseJSON also uses Codable interface which lets remap json properties into swift properties if they don't match. Hope this helps.

i prefer to give a general solution not only for this condition

it is very simple just download and run this MACOS APP from GITHUB run it in your mac by XCODE and but your JSON in it,
it will make Models for any complex JSON

notes

1 if JSON keys have a capital character in the first it will be small , so after copying model you need to change it like the JSON

2 if two JSON objects have the same structure and the same key names it will be only one model

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