Here is my JSON
{
"myItems": {
"item1name": [{
"url": "google.com",
"isMobile": true,
"webIdString": "572392"
}, {
"url": "hulu.com",
"isMobile": false,
"webIdString": "sad1228"
}],
"item2name": [{
"url": "apple.com",
"isMobile": true,
"webIdString": "dsy38ds"
}, {
"url": "Facebook.com",
"isMobile": true,
"webIdString": "326dsigd1"
}, {
"url": "YouTube.com",
"isMobile": true,
"webIdString": "sd3dsg4k"
}]
}
}
The json can have multiple items (such as item1name, item2name, item3name,...) and each item can have multiple objects (the example has 2 objects for the first item and 3 objects for the second item).
Here is the structure I want it saved to (incomplete):
struct ServerResponse: Decodable {
let items: [MyItem]?
}
struct MyItem: Decodable {
let itemName: String?
let url: String?
let isMobile: Bool?
let webIdString: String?
}
In the example above, it would mean that the items
list should have five MyItem
objects. For example, these would be the MyItem
objects:
#1: itemName: item1name, url: google.com, isMobile: true, webIdString: 572392
#2: itemName: item1name, url: hulu.com, isMobile: false, webIdString: sad1228
#3: itemName: item2name, url: apple.com, isMobile: true, webIdString: dsy38ds
#4: itemName: item2name, url: Facebook.com, isMobile: true, webIdString: 326dsigd1
#5: itemName: item2name, url: YouTube.com, isMobile: true, webIdString: sd3dsg4k
What would be the best way for me to do this using Decodable? Any help would be appreciated. I think this may be similar to this problem: How to decode a nested JSON struct with Swift Decodable protocol?