JSON 解码器问题。我如何解析这些数据?

问题描述 投票:0回答:1

大家。我正在为 iOS 制作一个应用程序。我使用的 API 允许将城市的景点作为 JSON 获取。

这里是数据的例子:

{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "id":"7281004",
         "geometry":{
            "type":"Point",
            "coordinates":[]
         },
         "properties":{
            "xid":"N5661720423",
            "name":"М. В. Захарову",
            "dist":70.53949268,
            "rate":1,
            "osm":"node/5661720423",
            "kinds":"historic,monuments_and_memorials,interesting_places,monuments"
         }
      },
... 499 objects more

我在 SWIFT 中解析的解决方案是

import Foundation

struct SightResponse: Decodable, Hashable {
    let allInfo = [String]()
    enum CodingKeys: String, CodingKey{
        case features
        case type
        
        enum FeatureKey: String, CodingKey{
            case type
            case id
        }
    }
    
    
}
extension SightResponse{
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        let type = try values.decode(String.self, forKey: .type)
        let ContainsFeatures = values.contains(.features)
        let features = try values.decode([String: String].self, forKey: .features)
        //skips the features line
    }
}

此外,在这一行我尝试了不同的数据类型变体,例如

let features = try values.decode(String.self, forKey: .features)
或者
let features = try values.decode([String].self, forKey: .features)

我真的很困惑

我尝试更改解析数据类型,但没有新内容。 我试图了解我的 JSON 数据做错了什么,以及我如何了解将来应该使用哪种数据类型?

json swift jsondecoder jsonserializer
1个回答
0
投票

1。声明您的
Codable
数据类型以解析 JSON 对象

struct FeatureCollection: Codable {
    let type: String
    let features: [Feature]
}

struct Feature: Codable {
    let type: String
    let id: String
    let geometry: Geometry
    let properties: Properties
}

struct Geometry: Codable {
    let type: String
    let coordinates: [Double]
}

struct Properties: Codable {
    let xid: String
    let name: String
    let dist: Double
    let rate: Int
    let osm: String
    let kinds: String
}

2。准备一些 JSON 模拟数据

// Some dummy data with 2 features in the array

let jsonString = """
{
    "type":"FeatureCollection",
    "features":[
        {
            "type":"Feature",
            "id":"1",
            "geometry":{
                "type":"Point",
                "coordinates":[]
            },
            "properties":{
                "xid":"N5661720423",
                "name":"М. В. Захарову",
                "dist":70.53949268,
                "rate":1,
                "osm":"node/5661720423",
                "kinds":"historic,monuments_and_memorials,interesting_places,monuments"
            }
        },
        {
            "type":"Feature",
            "id":"2",
            "geometry":{
                "type":"Point",
                "coordinates":[]
            },
            "properties":{
                "xid":"N5661720423",
                "name":"М. В. Захарову",
                "dist":70.53949268,
                "rate":1,
                "osm":"node/5661720423",
                "kinds":"historic,monuments_and_memorials,interesting_places,monuments"
            }
        }
    ]
}
"""

3。将模拟 JSON 数据解码为 Swift 对象

// Decoding

let jsonData = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let featureCollection = try! decoder.decode(FeatureCollection.self, from: jsonData)
© www.soinside.com 2019 - 2024. All rights reserved.