Advanced browsers such as FIREFOX,CHROME,OPERA,SAFARI,IE9,IE8 can directly use the stringify () and Parse () methods of the JSON object.
Json.stringify (obj) converts JSON to a string. Json.parse (String) converts a string to JSON format;
var a={"name": "Tom", "Sex": "Male", "Age": "A"};
var atostr = json.stringify (a);
alert (ATOSTR); Result: {"name": "Tom", "Sex": "Male", "Age": "24"}
Result: string
var b= ' {"name": "Tom", "Sex": "Male", "Age": "a";
var atoobj = Json.parse (b);
Console.log (Atoobj); Result: Object {name: "Tom", Sex: "Male", Age: "24"}
Result: Object
IE8 (compatibility mode), IE7 and IE6 do not have JSON objects, but http://www.json.org/provides a json.js so IE8 (compatibility mode), IE7 and IE6 can support JSON objects and their stringify () and parse () method;
Now generally use json2.js, download the address: click here.
Adding to the page will solve the problem of the IE low version without the JSON object.
<script type= "Text/javascript" src= "Js/json2.js" ></script>
The parse () and Stringfy () methods of Ps:json
1.json.parse;
Function: Converts a JSON string of JavaScript Object notation to an object (a string object)
Syntax: Json.parse (text [, Reviver])
Text must be selected. A valid JSON string.
Reviver Optional. A function that converts the result. This function will be called for each member of the object.
Return value: JSON object
Instance:
var jsonstr = ' {' name ': ' Leinov ', ' sex ': ' Famle ', ' address ': ' Beijing '} '
var jsonobj = Json.parse (JSONSTR);
Alert (typeof jsonobj); Object
2.json.stringify
Function: Converts a JavaScript JSON object to a JSON string of JavaScript Object Notation (object to String)
Syntax: json.stringify (value [, Replacer] [, space]);
Value required is usually an object or an array
Replacer a function or array of optional conversion results
Space is optional. Add indents, blanks, and newline characters to return the value JSON text is easier to read.
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 that specifies the 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 indents 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.
Instance:
var student = new Object ();
Student.name = "Leinov";
Student.sex = "Famle";
student.address = "Chaoyang";
var jsonstudent = json.stringify (student);
Alert (typeof jsonstudent); String