gpt4 book ai didi

json - 当从 Firebase 接收数组/字典时

转载 作者:行者123 更新时间:2023-12-02 17:30:37 25 4
gpt4 key购买 nike

我在 Firebase 中为社区设置了一个存储空间。由于我在我的 Swift 项目中定义了一个类,因此在从中生成对象时我需要知道它是数组还是字典。

我下载了 json 文件,它看起来像这样 - 2 个用户以不同的数据格式存储在 firebase 中,用于同一个“表”:

[{"user": {
"User1": {
"adminOf": {
"3": true,
"5": true
},
"alias": "borussenpeter",
"communities": {
"3": true,
"5": true
}
},
"User2": {
"adminOf": [null, true, true, null, true],
"alias": "burkart",
"communities": [null, true, true, null, true]
}
}}]

我尝试下载文件,对其进行编辑,使两个用户看起来都一样,然后再次上传,但 Firebase 以这种方式再次保存了它。

当然,当使用错误的数据类型时,初始化对象会失败。对此有什么想法吗?谢谢

最佳答案

答案在 Firebase documentation on arrays 中:

However, to help developers that are storing arrays in a Firebase database,... if the data looks like an array, Firebase clients will render it as an array. In particular, if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase clients will render it as an array.

在 User1 的 JSON 中,5 个索引有 2 个值(3 和 5)。这还不到键的一半,因此 Firebase 不会将其呈现为数组。

在 User2 的 JSON 中,您有 5 个索引的 3 个值(2、3 和 5)。这是键的一半以上,因此 Firebase 将其呈现为数组。

几种处理方法:

  • 用字符串作为整数前缀,例如“group3”:true
  • 向“数组”添加一个虚拟非整数:例如“未使用”:false
  • 存储所有索引的值:例如"group0": false

关于json - 当从 Firebase 接收数组/字典时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33880157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com