使用Codable解析嵌套的JSON

编程入门 行业动态 更新时间:2024-10-05 07:26:27
本文介绍了使用Codable解析嵌套的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

因此,我正在尝试在Swift中使用Codable解析看起来像这样的JSON.

So I'm trying to parse JSON that looks something like this using Codable in Swift.

{ "abilities": [ { "ability": { "name": "chlorophyll", "url": "pokeapi.co/api/v2/ability/34/" }, "is_hidden": true, "slot": 3 }, { "ability": { "name": "overgrow", "url": "pokeapi.co/api/v2/ability/65/" }, "is_hidden": false, "slot": 1 } ], "name": "SomeRandomName" }

现在,当您尝试获取嵌套数据时,它会造成混乱.现在,我正在尝试获取名称,这很容易.我也在尝试获得能力名称,这对我来说很复杂.经过研究,这就是我想出的.

Now it gets confusing when you're trying to get nested data. Now I'm trying to get the name, which is easy. I'm also trying to get the ability name, this is where its gets complicated for me. After some research this is what I came up with.

class Pokemon: Codable { struct Ability: Codable { var isHidden: Bool struct AbilityObject: Codable { var name: String var url: String } var ability: AbilityObject private enum CodingKeys: String, CodingKey { case isHidden = "is_hidden" case ability } } var name: String var abilities: [Ability] }

现在有什么更好的方法可以执行此操作,否则我会坚持执行此操作吗?

Now is there any better way in doing this, or am I stuck doing it like this.

推荐答案

获取您的JSON响应并将其转储到此网站.

Grab your JSON response and dump it in this site.

它将在没有Codable的情况下生成这些结构.添加Codable,使它们看起来像这样:

It'll generate these structs without Codable. Add Codable so they look like this:

struct Pokemon: Codable { let abilities: [AbilityElement] let name: String struct AbilityElement: Codable { let ability: Ability let isHidden: Bool let slot: Int struct Ability: Codable { let name: String let url: String } } }

对于带有snake_case的键,您只需声明一个JSONDecoder并将keyDecodingStrategy指定为.convertFromSnakeCase.如果您只是从蛇皮箱转换而来,则无需弄乱编码键.重命名密钥时才需要它们.

For keys with snake_case, you can just declare a JSONDecoder and specify the keyDecodingStrategy as .convertFromSnakeCase. No need to muck around with coding keys if you're just converting from snake case. You only need them if you're renaming keys.

如果在其他情况下需要为响应创建自定义编码密钥或更改密钥名称,请此页面应该会有所帮助.

If you have other situations where you need to create custom coding keys for your responses or alter key names, this page should prove helpful.

您可以将其丢弃在操场上并玩耍:

You can dump this in a playground and play around with it:

let jsonResponse = """ { "abilities": [ { "ability": { "name": "chlorophyll", "url": "pokeapi.co/api/v2/ability/34/" }, "is_hidden": true, "slot": 3 }, { "ability": { "name": "overgrow", "url": "pokeapi.co/api/v2/ability/65/" }, "is_hidden": false, "slot": 1 } ], "name": "SomeRandomName" } """ struct Pokemon: Codable { let abilities: [AbilityElement] let name: String struct AbilityElement: Codable { let ability: Ability let isHidden: Bool let slot: Int struct Ability: Codable { let name: String let url: String } } } var pokemon: Pokemon? do { let jsonDecoder = JSONDecoder() jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase if let data = jsonResponse.data(using: .utf8) { pokemon = try jsonDecoder.decode(Pokemon.self, from: data) } } catch { print("Something went horribly wrong:", error.localizedDescription) } print(pokemon)

更多推荐

使用Codable解析嵌套的JSON

本文发布于:2023-11-26 23:25:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   Codable   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!