问题标签 [decodable]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1358 浏览

json - Swift Decodable 的可失败初始化器

我正在尝试解析以下 json 架构,海报可能为空,也可能不为空

我的可解码类如下:

我的问题是如何使海报可选?我的想法是我需要一个可失败的初始化程序,但可解码需要一个抛出的 init。

0 投票
2 回答
3495 浏览

json - 动态 JSON 解码 Swift 4

我正在尝试在 Swift 4 中解码以下 JSON:

问题是,JSON 中的最后 2 个元素(display_namedevice_id)可能存在也可能不存在,或者元素可能被命名为完全不同但仍然未知的东西,即"fred": "worker", "hours" : 8

所以我想要实现的是解码已知的内容,即,token和任何其他元素(等)将它们放入字典中。permissiontimeout_inissuerdisplay_namedevice_id

我的结构如下所示:

如果有人可以提供一些指导,那么能够解码 JSON 也可以包含动态信息的已知结构的一部分就是我所处的位置。

谢谢

0 投票
2 回答
2533 浏览

json - 具有随机键属性的 Swift 4 可解码嵌套 json

我在解码 json 时遇到问题。我已经学习了很多教程,但没有使用复杂的 json 结构。为简单起见,我将代码最小化并使用 Dog 作为示例。

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

我有以下结构:

收集 Dog 结构:

我需要在我的“DogResponseData”结构中做什么才能快速识别包含我的 Dog 结构的随机变量?

0 投票
1 回答
1349 浏览

swift4 - Swift decodable:检查是否存在嵌套结构

我有一个类似这样的 JSON 数据。我使用了一种单独的结构方法,而不是在一个单独的结构中使用嵌套键。需要注意的一点是Given Json中的key不一致,可能不存在。因此,在尝试使用构建的结构解析它之前,必须对每个键进行检查。

解析这个我已经创建了像这样的快速结构

0 投票
1 回答
2963 浏览

ios - How to get the nondecoded attributes from a Decoder container in Swift 4?

I'm using the Decodable protocol in order to parse JSON received from an external source. After decoding the attributes that I do know about there still may be some attributes in the JSON that are unknown and have not yet been decoded. For example, if the external source added a new attribute to the JSON at some future point in time I would like to hold onto these unknown attributes by storing them in a [String: Any] dictionary (or an alternative) so the values do not get ignored.

The issue is that after decoding the attributes that I do know about there isn't any accessors on the container to retrieve the attributes that have not yet been decoded. I'm aware of the decoder.unkeyedContainer() which I could use to iterate over each value however this would not work in my case because in order for that to work you need to know what value type you're iterating over but the value types in the JSON are not always identical.

Here is an example in playground for what I'm trying to achieve:

I would like for the unknownAttributes dictionary to store the age attribute and value in this case and any other possible value types if they get added to the JSON from the external source in the future.

The reason I am wanting to do something like this is so that I can persist the unknown attributes present in the JSON so that in a future update of the code I will be able to handle them appropriately once the attribute keys are known.

I've done plenty of searching on StackOverflow and Google but haven't yet encountered this unique case. Thanks in advance!

0 投票
2 回答
1404 浏览

json - Swift 传递方法参数 Struct Decodable

,Swift 4 如何传入Decodable Struct方法参数并解析它JSONDecoder()?错误:

无法使用类型为“(可解码,来自:数据)”的参数列表调用“解码”

0 投票
1 回答
1826 浏览

swift - 可解码返回对象

我有一个可解码的类:

在错误类中,我有:

为了解码到这个类,我有:

由于我收到的一些回复是(成功回复):

然后一个失败的响应只包含一个错误对象,其中包含 desc 和代码。

我想要实现的是一个适用于两种场景的可解码类(当响应成功和失败时)但是我不知道如何实现这一点。我知道我可以制作 2 个单独的可解码类,但这会使事情变得更加混乱,因为我必须确定响应是否是错误并填充以返回不同的类。

有谁知道我应该如何实现这个>

0 投票
1 回答
2533 浏览

swift - 将 Decodable 与继承一起使用会引发异常

我正在使用 Rest API 服务,其中响应分为基本响应,所有其他响应都继承自它。

我正在尝试使用解码器接口为我的响应模型类构建相同的结构。

但是,我在解码继承类时遇到问题。

我试图关注这个问题: Using Decodable in Swift 4 with Inheritance

但没有运气。

这是初始结构:

这是我尝试解码的方式:

正如我上面写的,这一行抛出异常(在 BaseResponse 类中)

不知道如何处理它。

提前致谢!

0 投票
1 回答
1345 浏览

ios - 使用 KeyedDecodingContainer 对具有随机密钥的对象进行解码

我有一组看起来像这样的对象,我想解码

所以我正在尝试这些方面的东西

显然这是行不通的,因为字符串大小写 forwork不是真正的值。一位朋友建议使用KeyedDecodingContainer来获取钥匙,但不知道从哪里开始。希望在这里得到一些帮助。

谢谢!

0 投票
1 回答
290 浏览

ios - ios从json urlsession dataTask获取可解码对象参数

从 URLSession dataTask 的这个响应中,我如何转置收到的数据:

对这个结构使用语句

它属于数据类型,但我只需要一个数据表,而不是两者,而且我无法为收到的 jsonData 下标。我无法为 from: 参数获取正确的格式,我知道使用 REST API 和 JSON,并且可以真正使用帮助。谢谢