如何将具有可变变量名称的JSON转换为swift对象? [英] How do I convert JSON with variable variable names into swift object?

查看:43
本文介绍了如何将具有可变变量名称的JSON转换为swift对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个API进行的项目,该API给了我一个JSON对象,并在变量中包含了一些数据.但是,提供数据所依据的变量的名称每天都在变化.例如,今天的名称为285,明天的名称为286.如何将其转换为Swift对象?我已经写了一些代码,所以在这里:

I am working on a project with an API wich gives me a JSON Object with some data in a variable. But the name of the variable under which the data is provided changes from day to day. For example today the name will be 285 and tomorrow it‘ll be 286. How can I convert this into a Swift object? I‘ve alreade written some code so here it is:

获取数据部分:

func getData(){
        let semaphore = DispatchSemaphore.init(value: 0)

        let url = URL(string: URL_STRING)!
        var request = URLRequest(url: url)

        request.httpMethod = "GET"


        session.dataTask(with: request) { (dat, res, err) in

            print("Anfrage gestellt")

            guard let data = dat else {
                print("no Data")
                return
            }

            guard let resp = res else {
                print("No Response ")
                return
            }

            if let error = err {
                print("Error: \(error)")
                return
            }



            do{
                let decodedData = try self.decoder.decode(Report.self, from: data)
                print(decodedData)


            } catch {
                print("Data decode failed \(error)")
            }

            semaphore.signal()
        }.resume()
        semaphore.wait()

        return
    }

将在其中进行转换的对象

The object in which it will be convertet

class Report: Codable{

    var keys: [String] = []
    var latest: String?
    let s: S

    init(keys: [String], s: S){
        self.keys = keys
        self.s = s
        latest = keys[keys.count - 1]
    }

    enum CodingKeys: String, CodingKey{
        case s = "286"
    }

}

JSON对象:

{
  "285": {
    "AT": {
      "av": -72.1,
      "ct": 113986,
      "mn": -100.813,
      "mx": -27.115
    },
    "First_UTC": "2019-09-15T01:13:15Z",
    "HWS": {
      "av": 4.347,
      "ct": 54297,
      "mn": 0.20600000000000002,
      "mx": 21.272
    },
    "Last_UTC": "2019-09-16T01:52:49Z",
    "PRE": {
      "av": 742.003,
      "ct": 89613,
      "mn": 723.2129,
      "mx": 757.8722
    },
    "Season": "spring",
    "WD": {
      "1": {
        "compass_degrees": 22.5,
        "compass_point": "NNE",
        "compass_right": 0.382683432365,
        "compass_up": 0.923879532511,
        "ct": 1
      },
      "10": {
        "compass_degrees": 225.0,
        "compass_point": "SW",
        "compass_right": -0.707106781187,
        "compass_up": -0.707106781187,
        "ct": 6973
      },
      "11": {
        "compass_degrees": 247.5,
        "compass_point": "WSW",
        "compass_right": -0.923879532511,
        "compass_up": -0.382683432365,
        "ct": 3196
      },
      "12": {
        "compass_degrees": 270.0,
        "compass_point": "W",
        "compass_right": -1.0,
        "compass_up": -0.0,
        "ct": 3066
      },
      "3": {
        "compass_degrees": 67.5,
        "compass_point": "ENE",
        "compass_right": 0.923879532511,
        "compass_up": 0.382683432365,
        "ct": 131
      },
      "5": {
        "compass_degrees": 112.5,
        "compass_point": "ESE",
        "compass_right": 0.923879532511,
        "compass_up": -0.382683432365,
        "ct": 680
      },
      "6": {
        "compass_degrees": 135.0,
        "compass_point": "SE",
        "compass_right": 0.707106781187,
        "compass_up": -0.707106781187,
        "ct": 9405
      },
      "7": {
        "compass_degrees": 157.5,
        "compass_point": "SSE",
        "compass_right": 0.382683432365,
        "compass_up": -0.923879532511,
        "ct": 8813
      },
      "8": {
        "compass_degrees": 180.0,
        "compass_point": "S",
        "compass_right": 0.0,
        "compass_up": -1.0,
        "ct": 8231
      },
      "9": {
        "compass_degrees": 202.5,
        "compass_point": "SSW",
        "compass_right": -0.382683432365,
        "compass_up": -0.923879532511,
        "ct": 13801
      },
      "most_common": {
        "compass_degrees": 202.5,
        "compass_point": "SSW",
        "compass_right": -0.382683432365,
        "compass_up": -0.923879532511,
        "ct": 13801
      }
    }
  },
    "sol_keys": [
    "285"
  ]
}

感谢您的帮助

埃里克

推荐答案

如上所述,您可以将其解码为字典,首先为要解码的数据定义一个结构

As mentioned you can decode this as a dictionary, first define a struct for the data you want to decode

struct ReportData: Decodable {
    let at: SomeData
    let firstUTC: Date
    let hws: SomeData
    //...
}

然后将其解码为

let decodedData = try self.decoder.decode([String:ReportData].self, from: data)

要在字典中找到正确的键,就好像您可以使用sol_keys

to find the right key in the dictionary it looks like you can use sol_keys

if let keys = decodedData ["sol_keys"] {
    for key in keys {
        let report = decodeData[key]
        //...

这篇关于如何将具有可变变量名称的JSON转换为swift对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆