使用 JSON 数组与使用 JSON 对象有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12288820/
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
What are the differences between using JSON arrays vs JSON objects?
提问by antonpug
What are the difference and advantages of using JSON arrays:
使用 JSON 数组的区别和优势是什么:
{
thing:[
{ },
{ }
]
}
versus JSON objects:
与 JSON 对象:
{
thing:{
{ },
{ }
}
}
采纳答案by Jeremy J Starcher
Not to sound like a smart ass, but well...
听起来不像是个聪明人,但好吧......
The difference between an arrayand an objectis that
anarray和 an的区别object在于
Objects are set up using a keyand valuelike...
对象是使用 akey和valuelike 设置的...
person.age = 15;
If the keyvalue is a variable, then one could access it like...
如果该key值是一个变量,那么可以像访问它一样访问它...
var key = "age";
alert(person[key]);
Arrays use an integer[1] index and take a value.
数组使用整数[1] 索引并取值。
player[1].score += 1000;
[1] Yes, I know ... in Javascript the integer index is really turned into a string behind the scenes. Ignore that. Think of arrays taking an integer value ESPECIALLY when you think of JSON.
[1] 是的,我知道……在 Javascript 中,整数索引实际上在幕后变成了字符串。忽略那个。当您想到 JSON 时,请考虑特别采用整数值的数组。
回答by megyewodi
Objects- key and value, Arrays- integer. When do you use this or that?
对象-键和值,数组-整数。你什么时候用这个或那个?
I think of arrays and objects as "is a/an" and "has a" respectively. Lets use "Fruit" as example.
我认为数组和对象分别是“is a/an”和“has a”。让我们以“水果”为例。
Every item in fruit array is a type of fruit.
Fruit 数组中的每一项都是一种水果。
array fruit : [orange, mango, banana]
array fruit : [orange, mango, banana]
. Arrays can contain objects,strings, numbers, arrays, but lets deal with only objects and arrays.
. 数组可以包含对象、字符串、数字、数组,但我们只处理对象和数组。
array fruit : [orange:[], mango:{}, banana:{}]
array fruit : [orange:[], mango:{}, banana:{}]
. You can see that orange is an array too. It implies any item that goes int orange is a type of orange, say: bitter_orange, mandarin, sweet_orange.
. 你可以看到 orange 也是一个数组。这意味着任何进入橙色的项目都是一种橙色,比如:bitter_orange、mandarin、sweet_orange。
for fruit object, any item in it is an attribute of fruit. thus the fruit has a
对于fruit对象,其中的任何一项都是fruit的一个属性。因此水果有一个
object fruit :{seed:{}, endocarp:{},flesh:{}}
This also implies that anything within the seed object should be property of seed, say: colour,
这也意味着种子对象内的任何东西都应该是种子的属性,比如:颜色、
回答by Daniel A. White
JSON arrays represent a collection of objects. In JS, theres a bunch of collection functions off of them such as slice, pop, push. Objects have just more raw data.
JSON 数组表示对象的集合。在 JS 中,有一堆集合函数,例如slice, pop, push。对象只有更多的原始数据。
回答by Mike Brant
The second form you show is actually not valid JSON, as each of the objects in the "thing" object would need some sort or property name to access it by.
您显示的第二种形式实际上不是有效的 JSON,因为“事物”对象中的每个对象都需要某种类型或属性名称来访问它。
To answer your question, the difference is that in the first case, you would access the objects in "thing" using array access like obj.thing[0]or obj.thing[1]. In the second case, if you had proper property declarations you would access like obj.thing.property
要回答您的问题,不同之处在于,在第一种情况下,您将使用像obj.thing[0]or 之类的数组访问来访问“事物”中的对象obj.thing[1]。在第二种情况下,如果您有适当的属性声明,您将像这样访问obj.thing.property
Generally in JSON array are used to store a grouping of like items, while object are used to contain grouping of different properties for a single item.
通常在 JSON 数组中用于存储一组相似的项目,而对象用于包含单个项目的不同属性的分组。
回答by norbitheeviljester
JSON is primarily a language that allows serializing javascript objects into strings. So upon deserializing a JSON string you should get a javascript object structure. If your json deserializes into an object that stores 100 objects called object1 to object100 then that's going to be very inconvenient. Most deserializers will expect you to have known objects and arrays of known objects so that they can convert the strings into the actual object structure in the language you're using. Also this is a question that the philosophy of object oriented design would answer you.
JSON 主要是一种允许将 javascript 对象序列化为字符串的语言。所以在反序列化一个 JSON 字符串时,你应该得到一个 javascript 对象结构。如果您的 json 反序列化为一个存储 100 个名为 object1 到 object100 的对象的对象,那么这将非常不方便。大多数反序列化器都希望您拥有已知对象和已知对象数组,以便它们可以将字符串转换为您使用的语言中的实际对象结构。这也是面向对象设计哲学会回答你的问题。
回答by Paul Sweatte
A JSON object can be transformed using toJSON:
可以使用toJSON以下方法转换 JSON 对象:
function kryptonite(key)
{
var replacement = {};
for(var __ in this)
{
if(__ in alias)
replacement[__] = this[__]
}
return replacement;
}
var alias = {"Clark":"","phone":""};
var contact = {
"Clark":"Kent",
"Kal El":"Superman",
"phone":"555-7777"
}
contact.toJSON = kryptonite;
var foo = JSON.stringify(contact)
A JSON array can be transformed using map:
可以使用map以下方法转换 JSON 数组:
var contact = {
"Clark":"Kent",
"Kal El":"Superman",
"phone":"555-7777",
"home":[{"present":"Metropolis"},{"past":"Krypton"},{"future":"Phantom Zone"}]
}
var filter = {"past":"","future":""}
function junction(value, index)
{
for (var __ in filter) if(value[__]) return value[__]
}
var island = contact.home.map(junction);

