Javascript 是否保留了 JSON 列表中元素的顺序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7214293/
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
Is the order of elements in a JSON list preserved?
提问by user437899
I've noticed the order of elements in a JSON object not being the original order.
我注意到 JSON 对象中元素的顺序不是原始顺序。
What about the elements of JSON lists? Is their order maintained?
JSON 列表的元素呢?他们的秩序得到维护了吗?
回答by Facebook Staff are Complicit
Yes, the order of elements in JSON arrays is preserved. From RFC 7159 -The JavaScript Object Notation (JSON) Data Interchange Format (emphasis mine):
是的,JSON 数组中元素的顺序被保留。来自RFC 7159 - JavaScript Object Notation (JSON) 数据交换格式 (重点是我的):
An object is an unorderedcollection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
An array is an orderedsequence of zero or more values.
The terms "object" and "array" come from the conventions of JavaScript.
对象是零个或多个名称/值对的无序集合,其中名称是字符串,值是字符串、数字、布尔值、空值、对象或数组。
数组是零个或多个值的有序序列。
术语“对象”和“数组”来自 JavaScript 的约定。
Some implementations do also preserve the order of JSON objects as well, but this is not guaranteed.
一些实现也确实保留了 JSON 对象的顺序,但这并不能保证。
回答by Hot Licks
The order of elements in an array ([]
) is maintained. The order of elements (name:value pairs) in an "object" ({}
) is not, and it's usual for them to be "jumbled", if not by the JSON formatter/parser itself then by the language-specific objects (Dictionary, NSDictionary, Hashtable, etc) that are used as an internal representation.
数组 ( []
)中元素的顺序保持不变。“对象” ( {}
)中元素(名称:值对)的顺序不是,并且它们通常被“混淆”,如果不是由 JSON 格式化程序/解析器本身然后由语言特定的对象(字典, NSDictionary、Hashtable 等)用作内部表示。
回答by Salam Barbary
Practically speaking, if the keys were of type NaN, the browser will not change the order.
实际上,如果键是 NaN 类型,浏览器不会改变顺序。
The following script will output "One", "Two", "Three":
以下脚本将输出“一”、“二”、“三”:
var foo={"3":"Three", "1":"One", "2":"Two"};
for(bar in foo) {
alert(foo[bar]);
}
Whereas the following script will output "Three", "One", "Two":
而以下脚本将输出“三”、“一”、“二”:
var foo={"@3":"Three", "@1":"One", "@2":"Two"};
for(bar in foo) {
alert(foo[bar]);
}
回答by Benjamin Atkin
Some JavaScript engines keep keys in insertion order. V8, for instance, keeps all keys in insertion order except for keys that can be parsed as unsigned 32-bit integers.
一些 JavaScript 引擎按插入顺序保存键。例如,V8将所有键都按插入顺序保存,除了可以解析为无符号 32 位整数的键。
This means that if you run either of the following:
这意味着,如果您运行以下任一操作:
var animals = {};
animals['dog'] = true;
animals['bear'] = true;
animals['monkey'] = true;
for (var animal in animals) {
if (animals.hasOwnProperty(animal)) {
$('<li>').text(animal).appendTo('#animals');
}
}
var animals = JSON.parse('{ "dog": true, "bear": true, "monkey": true }');
for (var animal in animals) {
$('<li>').text(animal).appendTo('#animals');
}
You'll consistently get dog, bear, and monkeyin that order, on Chrome, which uses V8. Node.js also uses V8. This will hold true even if you have thousands of items. YMMV with other JavaScript engines.
在使用 V8 的 Chrome 上,您将始终按此顺序获得dog、bear和monkey。Node.js 也使用 V8。即使您有数千个项目,这也将成立。YMMV 与其他 JavaScript 引擎。
回答by user123444555621
"Is the order of elements in a JSON list maintained?" is not a good question. You need to ask "Is the order of elements in a JSON list maintained when doing [...] ?" As Felix King pointed out, JSON is a textual data format. It doesn't mutate without a reason. Do not confuse a JSON string with a (JavaScript) object.
“是否保持了 JSON 列表中元素的顺序?” 不是一个好问题。您需要问“在执行 [...] 时,JSON 列表中元素的顺序是否保持不变?” 正如 Felix King 指出的那样,JSON 是一种文本数据格式。它不会无故变异。不要将 JSON 字符串与 (JavaScript) 对象混淆。
You're probably talking about operations like JSON.stringify(JSON.parse(...))
. Now the answer is: It depends on the implementation. 99%* of JSON parsers do not maintain the order of objects, and do maintain the order of arrays, but you might as well use JSON to store something like
您可能正在谈论诸如JSON.stringify(JSON.parse(...))
. 现在的答案是:这取决于实现。99%* 的 JSON 解析器不维护对象的顺序,并且维护数组的顺序,但您不妨使用 JSON 来存储类似
{
"son": "David",
"daughter": "Julia",
"son": "Tom",
"daughter": "Clara"
}
and use a parser that maintains order of objects.
并使用维护对象顺序的解析器。
*probably even more :)
*可能更多:)