firebase 中的 json 数据格式 - 是否支持数组?和/或,如果只支持对象,字典可以用整数编号吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11377011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
json data format in firebase - are arrays supported? And/Or, if only objects are supported, can dictionaries be numbered with integers?
提问by Reinsbrain
I am tinkering with firebase and curious about the data structure. Browsing to my database, firebase allows me to modify the structure and data in my database. But it seems that firebase only supports objects (and dictionaries for lists).
我正在修改 firebase 并对数据结构感到好奇。浏览我的数据库时,firebase 允许我修改数据库中的结构和数据。但似乎 firebase 只支持对象(和列表的字典)。
I want to know if arrays are supported. I would also like to know if dictionary items can be named with integers - the firebase interface only inserts strings as names which makes me concerned about ordering records.
我想知道是否支持数组。我还想知道字典项是否可以用整数命名 - firebase 接口只插入字符串作为名称,这让我担心排序记录。
Here is a sample of json created through firebase interface:
这是通过 firebase 接口创建的 json 示例:
{
"dg":{
"users":{
"rein":{
"searches":{
"0":{
"urls":"http://reinpetersen.com,http://www.reinpetersen.com",
"keyphrases":"rein petersen,reinsbrain,programmer turned kitesurfer"
}
}
},
"jacqui":{
"searches":{
"0":{
"urls":"http://www.diving-fiji.com,http://diving-fiji.com",
"keyphrases":"diving marine conservation, diving fiji"
}
}
}
},
"crawl_list":{
"1":{
"urls":"http://www.diving-fiji.com,http://diving-fiji.com",
"keyphrases":"diving marine conservation, diving fiji"
},
"0":{
"urls":"http://reinpetersen.com,http://www.reinpetersen.com",
"keyphrases":"rein petersen,reinsbrain,programmer turned kitesurfer"
}
}
}
}
Obviously, for my lists, I want the dictionary item names to be integers so i can ensure sorting is correct.
显然,对于我的列表,我希望字典项名称为整数,以便确保排序正确。
回答by Andrew Lee
You can save arrays into Firebase. For example:
您可以将数组保存到 Firebase 中。例如:
var data = new Firebase(...);
data.set(['x', 'y', 'z']);
Javascript Arrays are essentially just objects with numeric keys. When retrieving data, we automatically detect when a Firebase object has only numeric keys, and we return an array if that is the case.
Javascript 数组本质上只是带有数字键的对象。检索数据时,我们会自动检测 Firebase 对象何时只有数字键,如果是这种情况,我们将返回一个数组。
Note that for storing a list of data to which many people can append, an array is not a good choice, as multiple people writing to the same index in the array can cause conflicts. Instead, we have a "push" function which creates a chronologically-ordered unique ID for your data.
请注意,对于存储许多人可以追加的数据列表,数组不是一个好的选择,因为多人写入数组中的同一索引可能会导致冲突。相反,我们有一个“推送”功能,可为您的数据创建按时间顺序排列的唯一 ID。
Also, if you're intending to use the array as a way of ordering data, there's a better way to do that using our priorities. See the docs.
此外,如果您打算使用数组作为排序数据的一种方式,那么使用我们的优先级有更好的方法来做到这一点。请参阅文档。
回答by bennlich
The Firebase docs have a pretty good section on how to order your data: Ordered Data.
Firebase 文档有一个关于如何对数据进行排序的很好的部分:Ordered Data。
Just like JSON fields, Firebase fields can only be named with strings. It sounds like what you're looking for is setWithPriority(), which attaches sortable priority data to your fields, or push(), which is guaranteed to give your fields unique names, ordered chronologically. (More on lists and push()here.)
就像 JSON 字段一样,Firebase 字段只能用字符串命名。听起来您正在寻找的是setWithPriority(),它将可排序的优先级数据附加到您的字段,或者push(),它保证为您的字段提供唯一名称,按时间顺序排列。(更多关于列表和push()这里。)
You can also push()or set()arrays. For example,
你也可以push()或set()数组。例如,
new Firebase("http://gamma.firebase.com/MyUser").push(["cakes","bulldozers"]);
results in a tree like you'd expect, with MyUser receiving a uniquely named child who has children "0":"cakes"and "1":"bulldozers".
结果就像你期望的那样,MyUser 接收一个唯一命名的孩子,他有孩子"0":"cakes"和"1":"bulldozers".

