First, I'm assuming this is the data structure you want. If it's not, please update the question with your expected output data structure. (report[0][0].name is fragile and awkward to use in Swift. Create a struct that matches the data you would like to have. Then decode into that struct.)
struct ApiData: Decodable {
let report: [Details]
}
struct Details {
let name: String
let value1: Int
let value2: Int
}
So each Details has exactly a name object and two integer values. And a report is a list of Details.
given that, you can hand-decode Details using a unkeyedContainer. To get the name field out, it is handy to make a helper struct:
extension Details: Decodable {
struct NameField: Decodable {
let name: String
}
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
self.name = try container.decode(NameField.self).name
self.value1 = try container.decode(Int.self)
self.value2 = try container.decode(Int.self)
}
}
Your comment notes "there is some more below name." In that case, you may want an object rather than a string here. In that case, it would look like this:
struct Person: Decodable {
let name: String
}
struct Details {
let person: Person
let value1: Int
let value2: Int
}
extension Details: Decodable {
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
self.person = try container.decode(Person.self)
self.value1 = try container.decode(Int.self)
self.value2 = try container.decode(Int.self)
}
}
Finally, it's possible that the list of values is unbounded, and you want an array:
struct Details {
let person: Person
let values: [Int]
}
In that case you'd decode by extracting all the values out of the unkeyedContainer:
extension Details: Decodable {
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
self.person = try container.decode(Person.self)
var values: [Int] = []
while !container.isAtEnd {
values.append(try container.decode(Int.self))
}
self.values = values
}
}
There are lots of ways to approach this. It all depends on how you want to use the data, and how much structure the data has.
let report: [[Any]]which isn't something you can use with Codable unless maybe with a custom init(from:). Is it your json, can you change the format?63in yourDetailsstructure? What about7?Detailsstruct to look like? The one you've given here just has name? And how manyDetailsstructs do you expect?decodeData.report[0][0].namefor arbitrary JSON, but the code is much more complex and harder to use than building a decoder that works for a known structure of data.