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.
Instance code:
var milasurlarr=new array ();//Create a new array
var submilasurlarr=new array ();//Create a new sub-array
$ (". InnerTable1 tbody TR"). each (function () {//Traverse table row
$ (this). Find ("input"). each (function () {
Submilasurlarr.push ($ (this). Val ());//gets each table input data in each row and stores it in a sub-array
})
Milasurlarr.push (Submilasurlarr);//depositing the subarray data into the parent array
});
var milasurl={};
For (var item in Milasurlarr) {
Milasurl[item]= Milasurlarr[item]; This loops the properties in the Milasurlarr array including the method copy to the Milasurl object.
}
Alert (Json.stringify (Milasurl));
JS in JS Array, the conversion between the object and JSON