JSON cognitive

First put authority: JSON  , JSON in JavaScript .

Objects in JavaScript using the object literal notation:

var object_literal = {
    Name: "zs",
    Age: 18,
    Married: false
};

JSON string:

var json_str = '{"Name": "ls", "Age": 20, "Married": true}';

In JavaScript, switching between the two

// js json object to a String 
var object_to_jsonstr = the JSON.stringify (object_literal); 
Alert (object_to_jsonstr); 

// json string into the object js 
var jsonstr_to_object = the JSON.parse (json_str); 
Alert ( 
    jsonstr_to_object.Name + ' , '+ 
    jsonstr_to_object.Age +', '+ 
    jsonstr_to_object.Married 
);

Note that where the JavaScript object literal, the name attributes may be double quotes may be omitted; JSON string representing the name of the attribute portion, inside double quotes, is only part of the value of a string type with double quotes, other data types do not.
After two JSON string is converted into the following JavaScript object, different results:

(function(){
    var str_one = '{"married": false}';
    var str_two = '{"married": "false"}';

    var obj_one = JSON.parse(str_one);
    var obj_two = JSON.parse(str_two);

    alert((obj_one.married === false) + ',' + (obj_two.married === false));

}());

 

Reproduced in: https: //www.cnblogs.com/xl0715/p/3144637.html

Guess you like

Origin blog.csdn.net/weixin_34252090/article/details/94023498