我碰到了一堵墙,我找不到路。我有一个 JSON 文件,我正在尝试解码,但缺少一部分 JSON。即使我知道那里应该有数据,我总是得到零。
这是 JSON 文件的第一部分
{
"type": "FeatureCollection",
"name": "points",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature",
"properties": { "osm_id": "755",
"name": "Majorstuen",
"other_tags": ""bus"=>"yes","network"=>"Ruter","public_transport"=>"stop_position"" },
"geometry": { "type": "Point", "coordinates": [ 10.7160479, 59.9296625 ] } },
{ "type": "Feature",
"properties": { "osm_id": "1188",
"name": "Arnes",
"highway": "bus_stop",
"other_tags": ""ref:nsrq"=>"82139"" },
"geometry": { "type": "Point", "coordinates": [ 16.919517, 68.3459 ] } },
{ "type": "Feature",
"properties": { "osm_id": "1194",
"name": "Skjellneset",
"highway": "bus_stop",
"other_tags": ""ref:nsrq"=>"82148"" },
"geometry": { "type": "Point", "coordinates": [ 16.938766, 68.34916 ] } },
{ "type": "Feature",
"properties": { "osm_id": "1202",
"name": "Osen",
"highway": "bus_stop",
"other_tags": ""ref:nsrq"=>"82152"" },
"geometry": { "type": "Point", "coordinates": [ 16.952982, 68.352551 ] } }
]}
这是我的结构:
struct This:Codable {
let type: String?
let name: String?
let crs: CRS
let features: [Features]
struct CRS: Codable {
let type: String?
let properties: CRSProperties
struct CRSProperties: Codable {
let name: String?
enum CodingKeys: String, CodingKey {
case name
}
}
enum CodingKeys: String, CodingKey {
case type
case properties
}
}
struct Features: Codable {
let type: String?
let properties: FeaturesProperties
struct FeaturesProperties: Codable {
let osmId: String?
let name: String?
let highway: String?
let otherTags: String?
let geo: FeaturesPropertiesGeometry?
struct FeaturesPropertiesGeometry: Codable {
let type: String
let coordinates: [Double]
enum CodingKeys: String, CodingKey {
case type
case coordinates
}
}
enum CodingKeys: String, CodingKey {
case osmId = "osm_id"
case name
case highway
case otherTags = "other_tags"
case geo = "geometry"
}
}
enum CodingKeys: String, CodingKey {
case type
case properties
}
}
}
然后这是我用来解码 JSON 的方法
guard let asset = NSDataAsset(name: "NN") else {
print("NN JSON not here")
return
}
do {
let deco = JSONDecoder()
this = try deco.decode(This.self, from: asset.data)
}catch {
print("Error: (error)")
}
我的问题是我没有将几何数据放入"this:this"。如果我从"this"打印每一行,那么"geo"的结果总是为零。
这是一条印刷线。 "功能(类型:可选("功能"(,属性:mokkingaroud。this.Features.FeaturesProperties(osmId: Optional("10587816"(, name: Optional("YX Evje"(, 高速公路: nil, otherTags: Optional("\"amenity\"=>\"fuel\",,"\"branch\"=>\"Evje\",\"brand\"=>\"YX\",\"email\"=>\"evje@st.yx.no",\"fuel:adblue\"=>\"yes\",\"fuel:diesel\"=>\"yes\",\"fuel:HGV_diesel\"=>"yes",\"fuel:octane_95\"=>"yes",\"fuel:taxfree_diesel\"=>\"yes\",\"hgv\"=>\"yes\",\"phone\"=>\"+47 37 92 95 60",\"ref:yx\"=>\"561\"(,geo: nil((">
请提出任何建议。
乔纳斯
因为coordinates
不是我会使用String
:
struct This:Codable {
let name, type: String?
let features: [Features]
}
struct Features: Codable {
let type: String?
let properties: [Properties]
let geometry: [Geometry]
}
struct Properties:Codable {
let name, osm_id, other_tags: String?
}
struct Geometry:Codable {
let type: String?
let coordinates: [Double]
}