Talk about the mutual conversion between JSON objects and strings json.stringify (obj) and Json.parse (string) _javascript techniques

Source: Internet
Author: User
Tags string to json

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.