Convert ordinary objects to JSON-formatted objects

Source: Internet
Author: User

1, what is called JSON?
JSON is just a data format (it's not a new type of data)
var obj = {name: "China", age:5000};//-> object in normal format
var jsonobj = {"Name": "China", "age": Object in 5000};//->json format (as long as the property name of the ordinary object is "" (cannot be ') wrapped up, such format is our JSON-formatted object)
var data = [
{Name: "", Age: "},
{Name: "", Age: "}
];//-> normal two-dimensional arrays

var jsondata = [
{"Name": "", "Age": ""},
{"Name": "", "Age": ""}
];//->json formatted data

2, which provides us with some methods for manipulating JSON-formatted data in the Window browser object
->window. JSON
->stringify: Convert JSON-formatted/plain-format objects to JSON-formatted strings
->parse: Converting JSON-formatted strings to JSON-formatted objects

var data = [
{name: "John Doe", age:48},
{name: "Zhang San", age:84}
];

var str = json.stringify (data);//-> ' [{' name ': ' John Doe ', ' age ': 48},{' name ': ' Zhang San ', ' Age ': 84}] '
Console.log (Json.parse (str));

3. Questions about compatibility
In IE6, IE7, there is no JSON property in the window.
Console.log (window. JSON); The result of the output under ie6~7 is undefined

How do I convert a JSON-formatted string to a JSON-formatted object in an incompatible situation? Use Eval, but remember it's best to manually add parentheses around the left and right sides of the string
var str = ' [{' name ': ' John Doe ', ' age ': 48},{' name ': ' Zhang San ', ' Age ': 84}] ';
var data = eval ("(" + str + ")");//-> compatible we use Json.parse (str)
Console.dir (data);

Convert ordinary objects to JSON-formatted objects

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.