Duplicate keyword value in JSON

At JSON.org, the basic data structures that JSON represents are listed as

  • A collection of name / value pairs and
  • An ordered list of values.

I could not find anywhere whether the second member with the same name as the one that is already parsing the current object should (a) throw an exception or (b) replace the existing item.

Is it indicated anywhere?

What do existing parsers with duplicate names do?

EDIT: I'm looking to determine the right behavior for my parser.

+3
source share
4 answers

JSON - JavaScript , - . ; .

, . , , , "Barney".

-

$(function() {

    $('#myButton').click(function(e) 

    {

    var myJsonString =  "Person = {'firstName':'Fred','lastName':'Flintstone','firstName':'Barney'}";

    eval("(" + myJsonString + ")");

    alert(Person.firstName);

    });

});

, eval() . JSON eval() - .

+3

, , . .

Javascript.

var json = {};

// lets augment the object
json.one = 1;
json.one = 2; // it gets replaced
+1

, , , ( , ).:-) undefined JSON POV.

As a practical matter, the implementations I used make either one of the suggestions you mentioned, or "use the first."

No, I will not count on specific behavior, given that tools can choose what to do.

0
source

Since JSON is just a subset of Javascript, it depends a lot on the Javascript specification. I personally do not know what the answer is, but I would really like not to rely on behavior, if at all possible.

-1
source

Source: https://habr.com/ru/post/1703535/


All Articles