What is JSON?
JavaScript Object Notation (JavaScript objects Notation).
JSON is a lightweight data interchange format in which a JSON-formatted file can grow like this:
{ "name": "Hanzichi", "sex": "Male"}
It looks like Key-value's key-value pair, which is the object of JS? Yes, but at the same time JSON is not satisfied, I can not be the same as JS object, I have to have my own personality, so the key-value pair must be in double quotes ! The value of the values in the key-value pairs is also specified:
The JSON value can be:
- Number (integer or floating point)
- 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, no other, no like JS undefined, Nan,json refused to use.
How do I use JSON?
JSON is generally in the form of strings in the process of data interaction, so for JS, how to convert the JSON string and JS object between each other is particularly important.
- Eval Dafa (JSON string--JS object)
The Eval function is fast, but it compiles any javascirpt code, which can create a security problem. The use of Eval is based on the assumption that the incoming code parameter is reliable, and there are cases where the client may be untrusted. If security-based considerations are preferable to using a JSON parser, a JSON parser will only accept JSON text, so it is more secure, as shown below.
- Json.parse (JSON string--JS object)
The second parameter can be a function that can be used to revise the value:
var jsonstr = ' {' name ': ' Hanzichi ', ' sex ': ' Male ', ' age ': ' $ '; var obj = Json.parse (jsonstr, function (key, value) { if (Key = = = ' name ') { return ' my name is ' + value; }
- 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 specifies a property that needs to be converted to a JSON string (in the form of an array, the JS object property with the same name as the array will be converted):
var obj = {name: ' Hanzichi ', Sex: ' Male ', Age: '};var ' jsonstr = json.stringify (obj, [' name ']); Console.log (JSONSTR);
//{"name": "Hanzichi"}
The second parameter can also be a function, you can delete the attributes that match the criteria (or change the value of the property, no return indicates that the property is discarded, the value of return represents the value of the key in the JSON string)
var obj = {name: ' Hanzichi ', Sex: ' Male ', Age: '};var ' jsonstr = json.stringify (obj, function (key, value) { if (key = = = ' name ') { return ' my name is ' + value; } return value;}); Console.log (JSONSTR);
You can also have a third argument, which can be a number or a string.
If it is a number, it is indented, and the number is more than 10 and is processed by 10.
var obj = {name: ' Hanzichi ', Sex: ' Male ', Age: ' Ten '};var jsonstr = json.stringify (obj, NULL, 4); Console.log (JSONSTR); {// "name": "Hanzichi",// "sex": "male",// "age": "10"//}
It can also be a string that is prefixed with these strings in front of the property, and the same string length exceeds 10 to intercept 10:
var obj = {name: ' Hanzichi ', Sex: ' Male ', Age: '};var ' jsonstr = json.stringify (obj, null, ' pre '); Console.log (JSONSTR); c9/>//{//Pre "name": "Hanzichi",//Pre "sex": "male",//Pre "age": "10"//}
Here I have a question, I think the output should be the following form to Ah ...
{"Prename": "Hanzichi", "presex": "Male", "preage": "10"}
Trouble has to know the big can tell me ...
Summarize
Of course, the legendary IE8 (and the following) because some kind of defect can not use the Json.parse () and Json.stringify () method, and eval () is not safe, if you want to be compatible with them, it is possible to refer to json2.js.
Introduction to JSON and Usage code summary