What is JSON? JavaScript Object Notation (JavaScriptObjectNotation ). JSON is a lightweight data exchange format. For example, a file in a JSON format can grow like this:
What is JSON?
JavaScript Object Notation (JavaScript Object Notation ).
JSON is a lightweight data exchange format. For example, a file in a JSON format can grow like this:
{ "name": "hanzichi", "sex": "male"}
It looks like key-value pairs, just like JavaScript objects? Yes, but at the same time, JSON indicates that I am not satisfied. I cannot grow like a js object. I have to have my own personality, so it is required that the key in the key-value pair must use double quotation marks! It is also required that the values in the key-value pair have certain requirements:
The JSON value can be:
Number (integer or floating point number)
String (in double quotation marks)
Logical value (true or false)
Array (in square brackets)
Object (in curly brackets)
Null
In addition to the above 6 types, there are no other types, and there is no undefined or NAN like js. JSON is not used.
How to Use JSON?
JSON is generally used as a string in the data interaction process. so for Javascript, it is particularly important to convert json strings and js objects.
Eval method (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 external cirpt code, which may cause security issues. The use of eval is based on the assumption that the input code parameters are reliable. In some cases, the client may be untrusted. For security reasons, it is best to use a JSON parser. a json parser will only accept JSON text, so it is safer, as shown below.
JSON. parse (json string-> js object)
var jsonStr = '{"name": "hanzichi", "sex": "male"}';var obj = JSON.parse(jsonStr);console.log(typeof obj, obj); // object Object {name: "hanzichi", sex: "male"}
The second parameter can be a function. You can delete or modify the value:
var jsonStr = '{"name": "hanzichi", "sex": "male", "age": 10}';var obj = JSON.parse(jsonStr, function(key, value) { if(key === 'name') { return 'my 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: '10'};var jsonStr = JSON.stringify(obj);console.log(jsonStr); // {"name":"hanzichi","sex":"male","age":"10"}
You can also add a parameter to specify the attributes that need to be converted to json strings (in the array form, js object attributes with the same name as the array will be converted ):
var obj = {name: 'hanzichi', sex: 'male', age: '10'};var jsonStr = JSON.stringify(obj, ['name']);console.log(jsonStr); // {"name":"hanzichi"}
The second parameter can also be a function. You can delete and select a qualified attribute (or change the attribute value. If no return parameter is set, the attribute is discarded, and the return parameter indicates the value of the key in the json string)
var obj = {name: 'hanzichi', sex: 'male', age: '10'};var jsonStr = JSON.stringify(obj, function(key, value) { if(key === 'name') { return 'my name is ' + value; } return value;});console.log(jsonStr); // {"name":"my name is hanzichi","sex":"male","age":"10"}
The third parameter can also be a number or string.
If it is a number, it indicates indent. If the number is greater than 10, it is processed as 10.
var obj = {name: 'hanzichi', sex: 'male', age: '10'};var jsonStr = JSON.stringify(obj, null, 4);console.log(jsonStr); // {// "name": "hanzichi",// "sex": "male",// "age": "10"// }
It can also be a string, which is prefixed with these strings before the attribute. Similarly, if the length of a string exceeds 10, only 10 characters are intercepted:
var obj = {name: 'hanzichi', sex: 'male', age: '10'};var jsonStr = JSON.stringify(obj, null, 'pre');console.log(jsonStr); // {// pre"name": "hanzichi",// pre"sex": "male",// pre"age": "10"// }
Summary
Of course, the legendary ie8 (and below) cannot use JSON due to a defect. parse () and JSON. stringify () method, and eval () is not secure. To be compatible with them, you can reference json2.js.