Is the order of elements in a JSON list preserved?

后端 未结 5 848
半阙折子戏
半阙折子戏 2020-11-22 12:20

I\'ve noticed the order of elements in a JSON object not being the original order.

What about the elements of JSON lists? Is their order maintained?

相关标签:
5条回答
  • 2020-11-22 12:28

    Yes, the order of elements in JSON arrays is preserved. From RFC 7159 -The JavaScript Object Notation (JSON) Data Interchange Format (emphasis mine):

    An object is an unordered collection 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 ordered sequence of zero or more values.

    The terms "object" and "array" come from the conventions of JavaScript.

    Some implementations do also preserve the order of JSON objects as well, but this is not guaranteed.

    0 讨论(0)
  • 2020-11-22 12:38

    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.

    0 讨论(0)
  • 2020-11-22 12:47

    "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.

    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

    {
        "son": "David",
        "daughter": "Julia",
        "son": "Tom",
        "daughter": "Clara"
    }
    

    and use a parser that maintains order of objects.

    *probably even more :)

    0 讨论(0)
  • 2020-11-22 12:50

    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.

    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 monkey in 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.

    Demo here and here.

    0 讨论(0)
  • 2020-11-22 12:52

    Practically speaking, if the keys were of type NaN, the browser will not change the order.

    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]);
    }
    
    0 讨论(0)
提交回复
热议问题