I have hit a wall that I cant find the way around. I have a JSON file that I am trying to decode but there is a part of the JSON that is missing. I always get nil even though I know that there is supposed to be data there.
This is the first part of the JSON file
{
"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 ] } }
]}
Here is my Struct:
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
}
}
}
Then here is what I use to decode the 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)")
}
My problem is that I dont get the geometry data into "this: This". If I print every line then from "this" then the result for "geo" is always nil.
This is a printed line. "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))"
Any suggestions please.
Jonas