通过 Codable 解析 JSON 到数组
Parsing of JSON by Codable to array
我关注JSON:
[
{
"id": 1,
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
37.5741167,
55.7636592
]
},
"properties": {
"hintContent": "переулок Волков, 13с1",
"balloonContentHeader": "переулок Волков, 13с1"
}
]
我正在尝试使用 JSON解码器:
struct Point : Codable {
let id: Int
let type: String
let properties: Properties
}
struct Properties : Codable {
let hintContent: String
let balloonContentHeader: String
}
struct Points : Codable {
var data : [Point]
}
func parse(fileName: String) {
let url = Bundle.main.url(forResource: fileName, withExtension: "json")
let data = try? Data(contentsOf: url!)
if let jsonPoints = try? JSONDecoder().decode([Point].self, from: data!) {
print(jsonPoints)
}
}
怎么了?
确保你的 JSON 是有效的——这个缺少 }
。
将您的(有效)JSON 粘贴到 app.quicktype.io 中以生成模型,即。确保您的模型与 JSON 匹配。如果您的 JSON 无效,该网站也会向您发出警告。
始终使用 do/try/catch
而不是 try?
以便在 JSON 解码失败时可以得到有意义的错误。
let data = """
[
{
"id": 1,
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
37.5741167,
55.7636592
]
},
"properties": {
"hintContent": "t",
"balloonContentHeader": "t"
}
}
]
""".data(using: .utf8)
struct Point: Codable {
let id: Int
let type: String
let geometry: Geometry
let properties: Properties
}
// MARK: - Geometry
struct Geometry: Codable {
let type: String
let coordinates: [Double]
}
// MARK: - Properties
struct Properties: Codable {
let hintContent, balloonContentHeader: String
}
func parse(fileName: String) {
do {
let jsonPoints = try JSONDecoder().decode([Point].self, from: data!)
print(jsonPoints)
} catch {
print(error)
}
}
如果没有调整后的 JSON,您的原始代码将在 catch
块中生成此错误:
The given data was not valid JSON....Badly formed object around character 224.
我关注JSON:
[
{
"id": 1,
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
37.5741167,
55.7636592
]
},
"properties": {
"hintContent": "переулок Волков, 13с1",
"balloonContentHeader": "переулок Волков, 13с1"
}
]
我正在尝试使用 JSON解码器:
struct Point : Codable {
let id: Int
let type: String
let properties: Properties
}
struct Properties : Codable {
let hintContent: String
let balloonContentHeader: String
}
struct Points : Codable {
var data : [Point]
}
func parse(fileName: String) {
let url = Bundle.main.url(forResource: fileName, withExtension: "json")
let data = try? Data(contentsOf: url!)
if let jsonPoints = try? JSONDecoder().decode([Point].self, from: data!) {
print(jsonPoints)
}
}
怎么了?
确保你的 JSON 是有效的——这个缺少
}
。将您的(有效)JSON 粘贴到 app.quicktype.io 中以生成模型,即。确保您的模型与 JSON 匹配。如果您的 JSON 无效,该网站也会向您发出警告。
始终使用
do/try/catch
而不是try?
以便在 JSON 解码失败时可以得到有意义的错误。
let data = """
[
{
"id": 1,
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
37.5741167,
55.7636592
]
},
"properties": {
"hintContent": "t",
"balloonContentHeader": "t"
}
}
]
""".data(using: .utf8)
struct Point: Codable {
let id: Int
let type: String
let geometry: Geometry
let properties: Properties
}
// MARK: - Geometry
struct Geometry: Codable {
let type: String
let coordinates: [Double]
}
// MARK: - Properties
struct Properties: Codable {
let hintContent, balloonContentHeader: String
}
func parse(fileName: String) {
do {
let jsonPoints = try JSONDecoder().decode([Point].self, from: data!)
print(jsonPoints)
} catch {
print(error)
}
}
如果没有调整后的 JSON,您的原始代码将在 catch
块中生成此错误:
The given data was not valid JSON....Badly formed object around character 224.