/ / Decodifica di un JSON senza chiavi in ​​Swift 4: array, json, swift, swift4, jsondecoder

Decodifica di un JSON senza chiavi in ​​Swift 4 - array, json, swift, swift4, jsondecoder

Sto usando un'API che restituisce questo orribile JSON:

[
"A string",
[
"A string",
"A string",
"A string",
"A string",
…
]
]

Sto cercando di decodificare l'array nidificato usando JSONDecoder, ma non ha una sola chiave e non so davvero da dove iniziare ... Hai qualche idea?

Molte grazie!

risposte:

0 per risposta № 1

Se la struttura rimane la stessa, puoi usarla decodificabile approccio.

Per prima cosa crea un modello decodificabile come questo:

struct MyModel: Decodable {
let firstString: String
let stringArray: [String]

init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
firstString = try container.decode(String.self)
stringArray = try container.decode([String].self)
}
}

O se vuoi davvero mantenere la struttura di JSON, in questo modo:

struct MyModel: Decodable {
let array: [Any]

init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
let firstString = try container.decode(String.self)
let stringArray = try container.decode([String].self)
array = [firstString, stringArray]
}
}

E usalo in questo modo

let jsonString = """
["A string1", ["A string2", "A string3", "A string4", "A string5"]]
"""
if let jsonData = jsonString.data(using: .utf8) {
let myModel = try? JSONDecoder().decode(MyModel.self, from: jsonData)
}

1 per risposta № 2

Una possibile soluzione è usare il JSONSerialization, quindi potresti semplicemente scavare dentro tale json, facendo così:

import Foundation

let jsonString = "["A string",["A string","A string", "A string", "A string"]]"
if let jsonData = jsonString.data(using: .utf8) {
if let jsonArray = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [Any] {
jsonArray.forEach {
if let innerArray = $0 as? [Any] {
print(innerArray) // this is the stuff you need
}
}
}
}

1 per risposta № 3

Questo è un po 'interessante per la decodifica.

Non ne hai key. Quindi elimina la necessità di un involucro struct.

Ma guarda i tipi interiori. Ottieni una miscela di String e [String] tipi. Quindi hai bisogno di qualcosa che si occupi di questo tipo di miscela. Avresti bisogno di un enum per essere precisi.

// I"ve provided the Encodable & Decodable both with Codable for clarity. You obviously can omit the implementation for Encodable
enum StringOrArrayType: Codable {
case string(String)
case array([String])

init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
do {
self = try .string(container.decode(String.self))
} catch DecodingError.typeMismatch {
do {
self = try .array(container.decode([String].self))
} catch DecodingError.typeMismatch {
throw DecodingError.typeMismatch(StringOrArrayType.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Encoded payload conflicts with expected type"))
}
}
}

func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .string(let string):
try container.encode(string)
case .array(let array):
try container.encode(array)
}
}
}

Processo di decodifica:

let json = """
[
"A string",
[
"A string",
"A string",
"A string",
"A string"
]
]
""".data(using: .utf8)!

do {
let response = try JSONDecoder().decode([StringOrArrayType].self, from: json)
// Here, you have your Array
print(response) // ["A string", ["A string", "A string", "A string", "A string"]]

// If you want to get elements from this Array, you might do something like below
response.forEach({ (element) in
if case .string(let string) = element {
print(string) // "A string"
}
if case .array(let array) = element {
print(array) // ["A string", "A string", "A string", "A string"]
}
})
} catch {
print(error)
}