带有随机键属性的Swift 4可解码嵌套json [英] Swift 4 decodable nested json with random key attributes

查看:71
本文介绍了带有随机键属性的Swift 4可解码嵌套json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解码json时遇到问题.我遵循了很多教程,但是没有使用复杂的json结构.为简单起见,我最小化了代码,并以Dog为例.

I'm having problems decoding json. I've followed lots of tutorials but non use complex json structures. For simplicity I minimized the code and use Dog as example.

在以下json中,我主要只对Dog结构感兴趣. json数据"属性包含随机的狗名.所以我不能使用编码键,因为我不知道属性名称.

In following json i'm mostly only interested in the Dog structs. The json "Data" attribute contains random dog names. So I cannot use coding keys because I dont know the attribute name.

{
     "Response": "success"
     "BaseLinkUrl": "https://wwww.example.com",
     "Data": {
         "Max": {
             "name": "Max",
             "breed": "Labrador"
         },
         "Rocky": {
             "name": "Rocky",
             "breed": "Labrador"
         },
         ...
    }
}

我有以下结构:

struct DogResponse : Decodable {
    let data : DogResponseData

    enum CodingKeys: String, CodingKey {
        case data = "Data"
    }
}

struct DogResponseData: Decodable {
    let dog: Dog //this is a random variable name

    enum CodingKeys: String, CodingKey {
        case dog = "??random_variable_dog_name??"
    }
}

struct Dog: Decodable {
    let name: String
    let type: String

    enum CodingKeys: String, CodingKey {
        case name
        case type = "breed"
    }
}

收集Dog结构:

let dogResponse = try JSONDecoder().decode(DogResponse.self, from: data)
print(dogResponse)

我需要在"DogResponseData"结构中执行什么操作才能迅速识别包含我的Dog结构的随机变量?

What do I need to do in my "DogResponseData" struct for swift to recognize a random variable that contains my Dog struct?

推荐答案

一种可行的解决方案是编写一个自定义初始化程序,将字典解码为[String:Dog]并将值映射到数组

A possible solution is to write a custom initializer to decode the dictionaries as [String:Dog] and map the values to an array

struct Dog : Decodable {
    let name : String
    let breed : String
}

struct DogResponse : Decodable {
    let dogs : [Dog]

    private enum CodingKeys: String, CodingKey {
        case data = "Data"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        let data = try values.decode([String : Dog].self, forKey: .data)
        dogs = Array(data.values)
    }
}


let dogResponse = try JSONDecoder().decode(DogResponse.self, from: data)
print(dogResponse.dogs)

================================================ ==========================

===========================================================================

或者,如果您想保留字典结构,则它会更短

Or if you want to keep the dictionary structure it's still shorter

struct Dog : Decodable {
    let name : String
    let breed : String
}

struct DogResponse : Decodable {
    let dogs : [String : Dog]

    private enum CodingKeys: String, CodingKey {
        case dogs = "Data"
    }
}

这篇关于带有随机键属性的Swift 4可解码嵌套json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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