jQuery 如何在 JSON 中存储多条记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15487900/
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
How to store multiple records in JSON
提问by Kivylius
I wan't to know how store multiple entities in json file (Structure) I will want to find by id functions (JQuery/javascript) and easy sorting (0,1,2...200).
我不知道如何在 json 文件(结构)中存储多个实体,我想通过 id 函数(JQuery/javascript)和简单的排序(0,1,2...200)来查找。
Here my code:
这是我的代码:
{
"id" : 5,
"name" : "Jemmy overy",
"data" : {...},
"link" : "http:...",
}
回答by castis
Btw, The answer by Lix below is best if you're looking to pull them out via their index number.
顺便说一句,如果您希望通过索引号将它们拉出,那么下面 Lix 的答案是最好的。
Wrap them in square brackets!
将它们包裹在方括号中!
[{
"id" : 5,
"name" : "Jemmy overy",
"data" : {...},
"link" : "http:...",
},
{
"id" : 6,
"name" : "John Smith",
"data" : {...},
"link" : "http:...",
}]
回答by Lix
Well, the only way I can see to identify the JSON objects in that manner would be to use it's ID property as a key -
好吧,我可以看到以这种方式识别 JSON 对象的唯一方法是使用它的 ID 属性作为键 -
var a = {
'5':{
"id" : 5,
"name" : "AAA",
"data" : {...},
"link" : "http:..."
},'6':{
"id" : 6,
"name" : "BBB",
"data" : {...},
"link" : "http:..."
},
...
};
So you access them like an array -
所以你像数组一样访问它们 -
a['5']
With regard to sorting, I don't think that there is any native way to sort JSON objects, but there are some other posts on the site that provide a helping hand - Sorting JSON by values
关于排序,我认为没有任何本地方式来对 JSON 对象进行排序,但是站点上还有一些其他帖子提供了帮助 -按值对 JSON 进行排序