Reputation: 13
Being new to iOS, XCode I'm trying to create a structure to represent JSON data. However, regardless of what I try for defining "segments" (which consists of a int and an array of strings) XCode just errors out and when I try to follow suggested fixes it just generates other errors.
Anybody know how to actually define a structure for JSON that is named, e.g., not using "ANY", since all the name-value pairs and data types are known?
Example XCODE (one variation shown below, though dozens have been tried and generates errors):
struct Information: Decodable {
var entry: [Entry]
}
struct Entry: Decodable {
var section: Int
***ERROR HERE ->*** var segments: Array<var id: Int, var values: Array<String>>
}
Example JSON:
{
"entry": [
{
"section": 1,
"segments": [
{
"id": 1,
"values": ["1", "2", "3"]
},
{
"id": 2,
"values": [ "4", "5", "6" ]
}
]
},
{
"section": 2,
"segments": [
{
"id": 1,
"values": ["7", "8", "9"]
},
{
"id": 2,
"values": [ "a", "b", "c" ]
}
]
}
]
}
Upvotes: 0
Views: 59
Reputation: 285079
It's the same as on the top level: You have to create a struct for the lower level.
struct Information: Decodable {
let entry: [Entry]
}
struct Entry: Decodable {
let section: Int
let segments: [Segment]
}
struct Segment: Decodable {
let id: Int
let values: [String]
}
Upvotes: 1