【问题标题】:Missing part from the JSONJSON 中缺少的部分
【发布时间】:2019-09-22 16:32:43
【问题描述】:

我撞到了一堵墙,我找不到路。我有一个我正在尝试解码的 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”的结果总是为零。

这是一条印刷线。 “功能(类型:可选(“功能”),属性:mokkingaroud.This.Features.FeaturesProperties(osmId:可选(“10587816”),名称:可选(“YX Evje”),高速公路:无,其他标签:可选(“ \"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\",\"电话\ "=>\"+47 37 92 95 60\",\"ref:yx\"=>\"561\""), 地理位置:无)) "

请有任何建议。

乔纳斯

【问题讨论】:

    标签: json swift codable


    【解决方案1】:

    因为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]
    }
    

    【讨论】:

    • 这有帮助,我看到了我在那里做错了什么。我将几何体放置在结构中的错误位置。
    • 如果此答案对您有所帮助,请通过选中投票部分下方的灰色复选标记,确保您将其批准为正确答案。
    猜你喜欢
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    相关资源
    最近更新 更多