The string set is as follows:
-
- VaRData="
-
- {
-
- Root:
-
- [
-
- {Name: '1', value: '0 '},
-
- {Name: '000000', value: 'xi'an '},
-
- {Name: '000000', value: 'tongchuan '},
-
- {Name: '200', value: 'baoji City '},
-
- {Name: '000000', value: 'xianyang '},
-
- {Name: '200', value: 'weinan City '},
-
- {Name: '000000', value: 'yan'an '},
-
- {Name: '000000', value: 'hanzhong '},
- {Name: '200', value: 'yulin '},
-
- {Name: '123', value: 'ankang City '},
-
- {Name: '200', value: 'shangluo City '}
-
- ]
-
- }";
Here, based on the data type obtained asynchronously by jquery-JSON object and string, we will introduce the Processing Methods of the obtained results in two ways.
For the JSON string returned by the server, if the jquery asynchronous request does not provide a type description or is accepted as a string, an object processing is required. The method is not too troublesome, but the string is placed in eval (). This method is also suitable for obtaining JSON objects in the common ccipt mode. The following is an example:
- VaRDataobj=Eval("(" + Data + ")"); // converts to a JSON object
- Alert (dataobj. Root. Length); // output the number of root sub-objects
- $. Each (dataobj. Root, fucntion (idx, item ){
- If (Idx= 0 ){
- Return true;
- }
-
- // Output the name and value of each root sub-Object
- Alert ("name:" + item. Name + ", value:" + item. value );
- })
For the JSON string returned by the server, if the jquery asynchronous request sets the type (usually this Configuration Attribute) to "JSON", or uses $. the getjson () method does not need the eval () method to obtain the server response, because the result is already a JSON object. You only need to call this object directly. Here, $. the getjson method is used as an example to describe the data processing method:
- $. getjson ("http://gaoyusi.blog.163.com/", {Param: "gaoyusi"}, function (data) {
- // The data returned here is already a JSON object
- // The following operations are the same as those in the first case
- $. Each (data. Root, function (idx, item) {
- If ( idx == 0) {
- return true; // returns the same value as countinue, and returns the same value as break.
- }
-
- alert ("name:" + item. name + ", value:" + item. value);
-
- });
- });