GET 返回的数据与映射值的数据存储方式不同
Data from GET is returned differently then how the data is stored for map values
例如,我的数据是这样存储的:
当我从 URL 获取数据时(使用 REST API):
https://<redacted>.com/maps/a6c00d68a95a5043374c1017b982d8f0b3009179/data/003_teleportIns.json
结果如下所示:
[{"sequenceNumber":0,"x":8,"y":3},{"sequenceNumber":1,"x":3,"y":14},{"sequenceNumber":2,"x":11,"y":15}]
我期望的是数据看起来像这样:
{"0":{"sequenceNumber":0,"x":8,"y":3},"1":{"sequenceNumber":1,"x":3,"y":14},"2":{"sequenceNumber":2,"x":11,"y":15}}
由于我使用 kotlinx serialization 解码数据,因此 firebase returns 数据在尝试解码时会导致错误。
这是一个错误示例:
Unexpected JSON token at offset 199: Expected start of the object '{', but had ':' instead
JSON input: .....":5,"y":7}],
"003_teleportIns":[{"sequenceNumber":0,"x":8,"y.....
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 199: Expected start of the object '{', but had ':' instead
JSON input: .....":5,"y":7}],
"003_teleportIns":[{"sequenceNumber":0,"x":8,"y.....
那么是什么原因呢?为什么 firebase 以意想不到的方式返回我的数据?如果这是预期的,请指出文档。
如果结果中有顺序数字键,Firebase REST API(和 SDK)returns 将它们作为数组。没有办法改变这种行为,所以我通常做的是用一个短的 non-numeric 字符串作为这些键的前缀,以防止数组强制转换。所以"key_0"
,"key_1"
,等等
例如,我的数据是这样存储的:
当我从 URL 获取数据时(使用 REST API):
https://<redacted>.com/maps/a6c00d68a95a5043374c1017b982d8f0b3009179/data/003_teleportIns.json
结果如下所示:
[{"sequenceNumber":0,"x":8,"y":3},{"sequenceNumber":1,"x":3,"y":14},{"sequenceNumber":2,"x":11,"y":15}]
我期望的是数据看起来像这样:
{"0":{"sequenceNumber":0,"x":8,"y":3},"1":{"sequenceNumber":1,"x":3,"y":14},"2":{"sequenceNumber":2,"x":11,"y":15}}
由于我使用 kotlinx serialization 解码数据,因此 firebase returns 数据在尝试解码时会导致错误。
这是一个错误示例:
Unexpected JSON token at offset 199: Expected start of the object '{', but had ':' instead
JSON input: .....":5,"y":7}],
"003_teleportIns":[{"sequenceNumber":0,"x":8,"y.....
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 199: Expected start of the object '{', but had ':' instead
JSON input: .....":5,"y":7}],
"003_teleportIns":[{"sequenceNumber":0,"x":8,"y.....
那么是什么原因呢?为什么 firebase 以意想不到的方式返回我的数据?如果这是预期的,请指出文档。
如果结果中有顺序数字键,Firebase REST API(和 SDK)returns 将它们作为数组。没有办法改变这种行为,所以我通常做的是用一个短的 non-numeric 字符串作为这些键的前缀,以防止数组强制转换。所以"key_0"
,"key_1"
,等等