简体   繁体   English

使用Decodable解码具有不同键值对的JSON

[英]Decoding JSON with varying key-value pair using Decodable

I am attempting to parse JSON using Decodable that has the following structure and I am being thrown the error: 我正在尝试使用具有以下结构的Decodable解析JSON,但被抛出错误:

does not confirm to protocol "Decodable" 不确认协议“可解码”

The JSON structure looks as such: JSON结构如下所示:

{
    base = SGD;
    date = "2017-12-29";
    rates =     {
        AUD = "0.95769";
        BGN = "1.2205";
        THB = "24.414";
        TRY = "2.8372";
        USD = "0.74844";
        ZAR = "9.2393";
    };
}

Note that I have shrank the size of the JSON object for readability. 请注意,为了便于阅读,我已经缩小了JSON对象的大小。

The trouble here is that the rates are all different key-value pairs which are unlike the posts here and here . 这里的麻烦在于,费率都是不同的键值对,这与此处此处的帖子不同。 My code so far as such: 我的代码到目前为止:

struct Fixer: Decodable {
    let base: String
    let date: String
    let rates: [AnyObject]
}

//at dataTasks
do {
     let results = try JSONDecoder().decode(Fixer.self, from: data)
     print(results.base)
} catch error as NSError {
     print(error?.localizedDescription)
}

It would be helpful if anyone could advice under such JSON structure with differing key-value pairs, how should one write the struct? 如果有人可以在这种具有不同键值对的JSON结构下提出建议,那将很有帮助,一个人应该如何编写该结构?

My reference: video 我的参考: 视频

The value for key rates is a dictionary, not an array. 关键rates的值是字典,而不是数组。

The most reasonable way is to decode the object to a dictionary 最合理的方法是将对象解码为字典

let rates: [String:String]

Alternatively create a struct Rate (for example with members currency and value ) and write a custom initializer to map the dictionary ( key -> currency and value -> value ) to an array of Rate 或者,创建一个结构化Rate (例如,使用员currencyvalue ),并编写一个自定义初始化程序,以将字典( key -> currencyvalue -> value )映射到Rate数组

Note: Consider that neither unspecified AnyObject nor Any can be used as a destination type. 注意:请考虑未指定的AnyObjectAny不能用作目标类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Swift Decodable 解码具有动态键值的 JSON,键值名称在 JSON 模型中的其他地方找到 - Decoding JSON that has a dynamic key value with the key value name found elsewhere in JSON model using Swift Decodable JSON对象作为键值对中的键 - JSON object as key in key-value pair 如何使用json文件形成带有键值对的字典 - how to form a dictionary with key-value pair using a json file 使用Swift Decodable进行动态(但又称为另一个键值)JSON解码 - Dynamic (But known as another key value) JSON Decoding with Swift Decodable Python JSON添加键值对 - Python JSON add Key-Value pair 如何通过使用 json 查询匹配值来查找所有 json 键值对 - How to find all the json key-value pair by matching the value using json query 如何使用python替换json文件中的键的键值对 - how to replace a key-value pair for a key in json file using python 如何使用jq在JSON对象中的指定键之后插入键值对? - How to insert a key-value pair after a specified key in a JSON object using jq? JSON 数组解析键值对中缺少键的位置 - JSON Array parsing where key is missing in the key-value pair JSON.NET将完整的键值对解析为键 - JSON.NET Parsing Full Key-Value Pair as Key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM