I'm writing a program where I'm parsing JSON data that includes array of arrays, where the nested arrays have different object types (specifically, [[String, String, Int]]). For example,
{
"number": 5295,
"bets": [
[
"16",
"83",
9
],
[
"75",
"99",
4
],
[
"46",
"27",
5
]
]
}
I'm trying to use codable to help me parse the data, but when I try something like
struct OrderBook: Codable {
let number: Int
let bets: [Bet]
}
struct Bet: Codable {
let price: String
let sale: String
let quantity: Int
}
it gives me errors saying that
Expected to decode Dictionary
<String, Any>but found an array instead
How do I get around this? I can't declare an array of empty type.
[[Any]]not[[String, String, Int]]let bets: [[Any]]it says it "doesn't conform to protocol 'Decodable'"Foundationcan allow for arrays of ostensibly different types by converting between objects of the same superclass. This question may help you: stackoverflow.com/questions/24236492/…