? data in name/value pairs
? data separated by commas
? curly braces Save the object
Square brackets Save the array
var jsonstring = ' {' Bar ': ' Property ', ' Baz ': 3} ';//json string var jsobject = Json.parse (jsonstring); Convert to JSON object alert (Jsobject.bar); Take the value in JSON var st = json.stringify (jsobject); Convert to a JSON-type string
//json array type string value var jsonStr = ' [{' id ': "" "," open ": false," pId ":" 0 "," name ":" A department "},{" ID ":" "," "open": false, "pid": "0", "name": "A department"},{"ID": "011", "open": false, "pId": "The" "a department"},{"ID": "The", "open ": false," pid ":" 0 "," name ":" A department "},{" id ":" "," "open": false, "pid": "0", "name": "A department"}, {"id": "", "" open ": false, "pid": "0", "name": "A department"}, {"id": "" "," open ": false," pid ":" 0 "," name ":" A Department "}] '; Console.log (JSONSTR) /* [{"id": "Down", "open": false, "pId": "0", "name": "A department"}, {"ID": "The", "open": False, "pid": "0", "name": "A department"}, {"ID": "011", "open": false, "pid": "", "name": "A department"}, {"ID": "The", "open": False, "pid": "0", "name": "A department"}, {"id": "", "" open ": false," pid ":" 0 "," name ":" A department "}, {" id ":" 05 "," Open ": false," pid ":" 0 "," name ":" A department "}, {" id ":" "", "open": false, "pid": "0", "name": "A Department"}] */ var jsonobj = json.parse (JSONSTR);//Convert to JSON object /* [object Object],[object Object],[object object],[object object],[object object],[object object],[object object] */ for (var i = 0; i < jsonobj.length; i++) { console.log (JSONOBJ [i].id); //take the value in JSON } console.log (jsonobj) var jsonStr1 = Json.stringify (jsonobj) console.log (jsonstr1 + "jsonStr1") /* [{"id": "" "," open ": False, "pid": "0", "name": "A department"}, {"id": "A", "open": false, "pid": "0", "name": "A department"}, {"id": " 011 "," open ": false," pid ":"}, {"," name ":" A department "," ID ":" The "," open ": false," pid ":" 0 "," name ":" A Department "}, {"id": "", "open": false, "pid": "0", "name": "A department"}, {"id": "", "" open ": false," pid ":" 0 "," Name ":" A department "}, {" id ":", "open": false, "pId": "0", "name": "A Department"}]jsonstr1 */
Detailed
In the process of data transfer, JSON is passed in the form of text, which is a string, and JS is the JSON object, so the conversion between JSON object and JSON string is the key. For example:
JSON string:
var str1 = ' {' name ': ' cxh ', ' sex ': ' Man '} ';
JSON object:
var str2 = {"Name": "Cxh", "Sex": "Man"};
One, JSON string converted to JSON object
To use the above str1, you must first convert to a JSON object using the following method:
Convert from JSON string to JSON object
var obj = Eval_r (' (' + str + ') ');
Or
var obj = Str.parsejson (); Convert from JSON string to JSON object
Or
var obj = json.parse (str); Convert from JSON string to JSON object
Then, you can read this:
Alert (Obj.name);
Alert (Obj.sex);
Special Note: If obj is originally a JSON object, then using the eval () function after conversion (even if multiple conversions) is a JSON object, but there is a problem with the Parsejson () function (throwing a syntax exception).
Second, the JSON object can be converted to a JSON string using tojsonstring () or global Method Json.stringify ().
For example:
var last=obj.tojsonstring (); Convert a JSON object to a JSON character
Or
var last=json.stringify (obj); Convert a JSON object to a JSON character
alert (last);
In the above several methods, in addition to the Eval_r () function is JS self-contained, several other methods are from the Json.js package. The new version of JSON modifies the API to inject json.stringify () and Json.parse () two methods into the Javascript built-in object, which becomes the object.tojsonstring (), and the latter becomes the Strin G.parsejson (). If you are prompted not to find the tojsonstring () and Parsejson () methods, your JSON package version is too low.
JS in JS Array, the conversion between the object and JSON