What is JSON?
- JSON refers to JavaScript Object notation (JavaScript Notation)
- JSON is a lightweight text data interchange Format
- JSON Independent of Language *
- JSON is self-descriptive and easier to understand
* JSON uses JAVASCRIPT syntax to describe data objects, but JSON is still independent of language and platform. JSON parser and JSON library support many different programming languages
JSON string:
var str = ' {' name ': ' Zwq ', ' sex ': ' Man '} '; |
JSON object:
var obj= {"Name": "Zwq", "Sex": "Man"}; |
JavaScript native parsing (JSON string-->json object)
The JSON text format is syntactically identical to the code that creates the JavaScript object.
Because of this similarity, the JavaScript program can use the built-in eval () function and the function object to generate native JavaScript objects with JSON data without a parser.
- 1. eval ();
var obj=eval ("(" +str+ ")");//Convert to JSON object |
- 2. Function object ;
var obj= (New Function ("", "Return" +str)) (); Convert to JSON object |
JSON object parsing
- 1. JSON string--- json object
Grammar:
Json.parse (text[, Reviver]) |
Parameter description:
Text: Required, a valid JSON string.
Reviver: Optional, a function that transforms the result that will call this function for each member of the object
return value :
Returns the converted object for the given JSON string.
Example :
var obj=json.parse (str); |
- 2. JSON Object--- json string
Grammar:
Json.stringify (value[, replacer[, space]) |
Parameter description:
Value: Required, a valid JSON object.
Replacer: Optional. The function or array used to convert the result.
If Replacer is a function, Json.stringify calls the function and passes in the key and value of each member. Use the return value instead of the original value. If this function returns undefined, the member is excluded. The key of the root object is an empty string: "".
If Replacer is an array, only the members with key values in the array are converted. The members are converted in the same order as the keys in the array. When the value parameter is also an array, replacer arrays are ignored.
Space: Optional, text adds indents, spaces, and line breaks, if space is a number, the return value text indents the specified number of spaces at each level, and if space is greater than 10, the text indents 10 spaces. Space has the ability to use non-numeric, such as: \ T.
return value :
Returns the converted string for the given JSON object.
Example :
var str=json.stringify (obj); |
JSON Conversion Toolkit
To facilitate the processing of JSON data, JSON provides the Json.js package: http://www.json.org
- 1. JSON string--- json object
var obj = Str.parsejson (); Convert from JSON string to JSON object |
- 2. JSON Object--- json string
var str=obj.tojsonstring (); To convert a JSON object to a JSON string |
JSON strings and JSON objects convert to and from each other