Недостающая часть из JSON - PullRequest
0 голосов
/ 22 сентября 2019

Я врезался в стену, которую не могу найти.У меня есть файл 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:

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.

Это печатная строка."Функции (тип: Необязательный (" Feature "), свойства: mokkingaroud.This.Features.FeaturesProperties (osmId: Необязательный (" 10587816 "), имя: Необязательный (" YX Evje "), шоссе: nil, otherTags: Необязательный (")\ "удобства \" => \ "топлива \", \ "филиал \" => \ "Evje \", \ "бренд \" => \ "YX \", \ "электронной почты \" => \ "Evje @st.yx.no \ "\ "топливо: AdBlue \"=> \ "да \", \ "топливо: дизель \"=> \ "да \", \ "топливо: HGV_diesel \"=> \" да\ "\ "топливо: octane_95 \"=> \ "да \", \ "топливо: taxfree_diesel \"=> \ "да \", \ "HGV \"=> \ "да \", \" телефон \"=> \" + 47 37 92 95 60 \ ", \" ref: yx \ "=> \" 561 \ ""), гео: ноль )) "

Любые предложения, пожалуйста.

Йонас

1 Ответ

0 голосов
/ 22 сентября 2019

Поскольку 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]
}
...