JSON (JavaScript Object notation) is a lightweight data interchange format, which is a JavaScript representation of objects. It is very convenient for programmers to deal with the data, but also convenient for the machine to the data analysis and generation, the application is very extensive.
JSON is a lightweight data interchange format in which a JSON-formatted file can grow like this:
{
' name ': ' Hanzichi ',
' sex ': ' Male '
}
It all seems to be Key-value's key-value pairs, much like the JS object? Yes, but at the same time JSON is not satisfied, I can not grow with the object of JS, I have to have my own personality, so the key-value pairs must be in double quotes! At the same time, the value of the values in the key-value pairs must be specified:
The JSON value can be:
Number (integer or floating-point numbers)
String (in double quotes)
Logical value (TRUE or FALSE)
Array (in square brackets)
Object (in curly braces)
Null
In addition to the above 6 kinds of, no other, no like JS undefined, Nan,json refused to use.
How do I use JSON?
JSON generally takes the form of a string in the data interaction process, so for JS, how to convert the JSON string and JS object to each other is particularly important.
Eval Dafa (JSON string-> JS object)
var jsonstr = ' {' name ': ' Hanzichi ', ' sex ': ' Male '} ';
var ans = eval (' (' + jsonstr + ') ');
Console.log (Ans.name, ans.sex); Hanzichi Male
The Eval function is very fast, but it can compile any javascirpt code, which could create a security problem. The use of Eval is based on the assumption that incoming code parameters are reliable, and in some cases, the client may not be trusted. If based on security considerations, it is best to use a JSON parser, a JSON parser will only accept JSON text, so it is more secure, as follows.
Json.parse (JSON string-> js object)
var jsonstr = ' {' name ': ' Hanzichi ', ' sex ': ' Male '} ';
var obj = json.parse (jsonstr);
Console.log (typeof obj, obj); Object {name: "Hanzichi", Sex: "Male"}
The second argument can be a function that can be censored for a value:
var jsonstr = ' {' name ': ' Hanzichi ', ' sex ': ' Male ', ' age ': ';
var obj = json.parse (jsonstr, function (key, value) {
if (key = = = ' name ') {return
' I name is ' + value
} return
value;
});
Console.log (typeof obj, obj); Object Object {name: ' My name is Hanzichi ', sex: ' Male ', age:10}
json.stringify (JS object-> JSON string)
var obj = {name: ' Hanzichi ', Sex: ' Male ', Age: ' Ten '};
var jsonstr = json.stringify (obj);
Console.log (JSONSTR); {"Name": "Hanzichi", "Sex": "Male", "Age": "10"}
You can also add a parameter that sets the attribute (array form, which has the same name as the array) that needs to be converted to a JSON string:
var obj = {name: ' Hanzichi ', Sex: ' Male ', Age: ' Ten '};
var jsonstr = json.stringify (obj, [' name ']);
Console.log (JSONSTR); {' name ': ' Hanzichi '}
The second parameter can also be a function that deletes the qualifying attribute (or changes the property value, no return indicates that the attribute is discarded, and the value of return indicates the value of the key in the JSON string)
var obj = {name: ' Hanzichi ', Sex: ' Male ', Age: ' Ten '};
var jsonstr = json.stringify (obj, function (key, value) {
if (key = = = ' name ') {return
' I name is ' + value
} return
value;
});
Console.log (JSONSTR); {' name ': ' My name is Hanzichi ', ' sex ': ' Male ', ' age ': ' 10 '}
You can also have a third parameter, either a number or a string.
If it's a number, it's indented, the number is over 10, and the size is 10.
var obj = {name: ' Hanzichi ', Sex: ' Male ', Age: ' Ten '};
var jsonstr = json.stringify (obj, NULL, 4);
Console.log (JSONSTR);
{
//"name": "Hanzichi",
//"sex": "male",//"Age
": "Ten"
//}
It can also be a string that will prefix the property with these strings, and the same string length over 10 intercepts 10:
var obj = {name: ' Hanzichi ', Sex: ' Male ', Age: ' Ten '};
var jsonstr = json.stringify (obj, null, ' pre ');
Console.log (JSONSTR);
{
//Pre ' name ': ' Hanzichi ',
//Pre ' sex ': ' Male ',
//Pre ' age ': ' Ten '
//}
Here I have a question, I think the output should be the following form of AH ...
{"
prename": "Hanzichi", "
presex": "Male",
"Preage": "Ten"
}
Trouble to know the big can portrait tell me ...
Summarize
Of course, the legendary IE8 (and below) because a flaw cannot use the json.parse () and the Json.stringify () method, and eval () is unsafe, you can reference json2.js if you want to be compatible with them.
The above content has introduced the JSON introduction as well as the usage summary, hoped that has the help to everybody!