gpt4 book ai didi

JSONDecoder 给出不正确的格式

转载 作者:行者123 更新时间:2023-11-30 10:47:46 25 4
gpt4 key购买 nike

我有一个 .json 文件,其中有一些 map ,还有一些带有位置的数组。我想将其解码为一个对象,但我不断收到错误消息,指出我的 .json 格式不正确。这可能很愚蠢,但如果有人可以提供帮助,我们将不胜感激。

JSON 文件

{
"Drenthe" : {
"gebieden" : {
"Oostermoer" : {
"latitude" : "52.3520406",
"longitude" : "4.6575235",
"name" : "Oostermoer"
}
},
"overige" : {
"Bergen op zoom" : {
"latitude" : "51.4918205",
"longitude" : "4.2931883",
"name" : "Bergen op zoom"
}
},
"plaatsen" : {
"Assen" : {
"latitude" : "52.9948311",
"longitude" : "6.5591529",
"name" : "Assen"
},
"Beilen" : {
"latitude" : "52.857755",
"longitude" : "6.5162114",
"name" : "Beilen"
},
"Borger" : {
"latitude" : "52.9243981",
"longitude" : "6.7912636",
"name" : "Borger"
}
},
"polders" : {
"Poldersemeer" : {
"latitude" : "51.82653820831839",
"longitude" : "4.257233124070814",
"name" : "Poldersemeer"
}
},
"provincies" : {
"Drenthe" : {
"latitude" : "52.90805",
"longitude" : "6.6888853",
"name" : "Drenthe"
}
},
"wateren" : {
"Drentsche Hoofdvaart" : {
"latitude" : "52.8420101",
"longitude" : "6.3372173",
"name" : "Drentsche Hoofdvaart"
}
}
},
"Flevoland" : {
"gebieden" : {
"Oostermoer" : {
"latitude" : "52.3520406",
"longitude" : "4.6575235",
"name" : "Oostermoer"
}
},
"overige" : {
"Bergen op zoom" : {
"latitude" : "51.4918205",
"longitude" : "4.2931883",
"name" : "Bergen op zoom"
}
},
"plaatsen" : {
"bierna" : {
"latitude" : "46.809093",
"longitude" : "7.62282",
"name" : "bierna"
},
"bosse" : {
"latitude" : "11.0559304",
"longitude" : "-14.4189868",
"name" : "bosse"
},
"dsaf" : {
"latitude" : "52.5168022",
"longitude" : "5.7156222",
"name" : "dsaf"
},
"isja" : {
"latitude" : "25.2934148",
"longitude" : "32.5525225",
"name" : "isja"
},
"lanes" : {
"latitude" : "43.9283286",
"longitude" : "-122.8976901",
"name" : "lanes"
}
},
"polders" : {
"Poldersemeer" : {
"latitude" : "51.82653820831839",
"longitude" : "4.257233124070814",
"name" : "Poldersemeer"
}
},
"provincies" : {
"Drenthe" : {
"latitude" : "52.90805",
"longitude" : "6.6888853",
"name" : "Drenthe"
}
},
"wateren" : {
"Drentsche Hoofdvaart" : {
"latitude" : "52.8420101",
"longitude" : "6.3372173",
"name" : "Drentsche Hoofdvaart"
},
"Hoogeveense Vaart" : {
"latitude" : "52.724406",
"longitude" : "6.5762008",
"name" : "Hoogeveense Vaart"
}
}
}
}

map 和位置结构

struct Location: Codable {
let name, latitude, longitude : String
}

struct Map: Codable {
var name: String?
var plaatsen: [Location]?
var provincies: [Location]?
var gebieden: [Location]?
var polders: [Location]?
var kanalen: [Location]?
var wateren: [Location]?
var overige: [Location]?

init(
name: String?,
plaatsen: [Location]?,
provincies: [Location]?,
gebieden: [Location]?,
polders: [Location]?,
kanalen: [Location]?,
wateren: [Location]?,
overige: [Location]?
)
{
self.name = name
self.plaatsen = plaatsen
self.provincies = provincies
self.gebieden = gebieden
self.polders = polders
self.kanalen = kanalen
self.wateren = wateren
self.overige = overige
}
}

检索 JSON

// Getting the maps.json file
if let path = Bundle.main.path(forResource: "maps", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
do {
let decoder = JSONDecoder()
let mapDecoded = try decoder.decode(Map.self, from: data)
print(mapDecoded)
} catch {
print("error trying to convert data to JSON")
print(error.localizedDescription)
}
} catch {
// handle error
print(error.localizedDescription)
}
}

编辑我不想使用类似 https://app.quicktype.io 的东西因为它们都有相同的格式,我想在我的 json 文件中放入更多 map

最佳答案

你需要

struct Map: Codable {
let drenthe: Drenthe

enum CodingKeys: String, CodingKey {
case drenthe = "Drenthe"
}
}

struct Drenthe: Codable {
let gebieden: Gebieden
let overige: Overige
let plaatsen: Plaatsen
let polders: Polders
let provincies: Provincies
let wateren: Wateren
}

struct Gebieden: Codable {
let oostermoer: Oostermoer

enum CodingKeys: String, CodingKey {
case oostermoer = "Oostermoer"
}
}

struct Oostermoer: Codable {
let latitude, longitude, name: String
}

struct Overige: Codable {
let bergenOpZoom: Oostermoer

enum CodingKeys: String, CodingKey {
case bergenOpZoom = "Bergen op zoom"
}
}

struct Plaatsen: Codable {
let assen, beilen, borger: Oostermoer

enum CodingKeys: String, CodingKey {
case assen = "Assen"
case beilen = "Beilen"
case borger = "Borger"
}
}

struct Polders: Codable {
let poldersemeer: Oostermoer

enum CodingKeys: String, CodingKey {
case poldersemeer = "Poldersemeer"
}
}

struct Provincies: Codable {
let drenthe: Oostermoer

enum CodingKeys: String, CodingKey {
case drenthe = "Drenthe"
}
}

struct Wateren: Codable {
let drentscheHoofdvaart: Oostermoer

enum CodingKeys: String, CodingKey {
case drentscheHoofdvaart = "Drentsche Hoofdvaart"
}
}
<小时/>
do {
let decoder = JSONDecoder()
let mapDecoded = try decoder.decode(Map.self, from: data)
print(mapDecoded)
} catch {
print(error) // << Don't forget this
}
<小时/>

同时使用

if let url = Bundle.main.url(forResource: "maps", withExtension: "json") { }

关于JSONDecoder 给出不正确的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55396003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com