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