JSON 中的缺失部分
Missing part from the JSON
我碰壁了,找不到路。我有一个要解码的 JSON 文件,但缺少 JSON 的一部分。我总是得到 nil,即使我知道那里应该有数据。
这是 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" 的结果总是 nil。
这是打印出来的一行。
"Features(type: Optional("Feature"), properties: mokkingaroud.This.Features.FeaturesProperties(osmId: Optional("10587816"), name: Optional("YX Evje"), highway: 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]
}
我碰壁了,找不到路。我有一个要解码的 JSON 文件,但缺少 JSON 的一部分。我总是得到 nil,即使我知道那里应该有数据。
这是 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" 的结果总是 nil。
这是打印出来的一行。 "Features(type: Optional("Feature"), properties: mokkingaroud.This.Features.FeaturesProperties(osmId: Optional("10587816"), name: Optional("YX Evje"), highway: 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]
}