(1) Convert JSON text to JavaScript object
because JSON syntax is a subset of JavaScript syntax, the JavaScript function eval () can be used to convert JSON text to JavaScript objects.
syntax: var obj = eval ("(" + Jsontxt + ")"); Where Jsontxt is the JSON text.
examples are as follows:
var jsontxt = ' {' name ': ' Leinov ', ' sex ': ' Famle ', ' address ': ' Beijing '} ' var jsonobj = eval ("(" +jsontxt+ "); Alert (typeof jsonobj); Object
Json.parse ();
Function: Converts a JSON string of JavaScript Object notation to an object (string to object)
Syntax: Json.parse (text [, Reviver])
Text must be selected. A valid JSON string.
Reviver is optional. A function that transforms the result. This function is called for each member of the object.
Examples are as follows:
var jsontxt = ' {"name": "Leinov", "Sex": "Famle", "Address": " Beijing "} ' var jsonobj = json.parse (jsontxt); alert (TYPEOF JSONOBJ); //object
(2) Convert the JSON object to a JSON string
Json.stringify ()
Role: A JSON string that converts a JavaScript JSON object to a JavaScript Object notation (object to String)
Syntax: json.stringify (value [, Replacer] [, space]);
Value must be selected. Typically an object or an array
Replacer is optional. function or array that transforms the result
Space is optional. Adding indents, whitespace, and newline characters returns the value of the JSON text for easier reading.
If space is omitted, the return value text is generated without any additional whitespace.
If space is a number, the return value has a blank text indent for the specified number at each level. If space is greater than 10 o'clock, the text indents 10 blanks.
If space is a non-empty string, such as "\ T", the return value text is indented with the character of the string at each level.
If space is a string greater than 10 characters, the first 10 characters are used.
Return value: The text string that the JSON contains.
var student = new Object (); Student.name = "Lenovointer"; Student.sex = "male"; student.address = "Hz"; var jsonstudent = json.stringify (student); Alert (typeof jsonstudent); String
This article from "front-end" blog, declined reprint!
JSON eval () and the Parse () method and the Stringfy () method function