The JSON data parsing method using jquery is used as the transmission object of jquery asynchronous requests. The result returned by the jquery request is a json object, which is considered in the form of a JSON string returned by the server, JSON objects encapsulated by plug-ins such as JSONObject are similar in that they are not described here. Here we first provide a JSON string set,
The JSON data parsing method using jquery is used as the transmission object of jquery asynchronous requests. The result returned by the jquery request is a json object, which is considered in the form of a JSON string returned by the server, JSON objects encapsulated by plug-ins such as JSONObject are similar in that they are not described here.
Here we first provide a JSON string set, which is as follows:
The Code is as follows:
- Var data ="
- {
- Root:
- [
- {Name:'1', Value:'0'},
- {Name:'123', Value:'Beijing'},
- {Name:'123', Value:'Tianjin'},
- {Name:'123', Value:'Shanghai'},
- {Name:'123', Value:'Chongqing'},
- {Name:'123', Value:'Weinan City'},
- {Name:'123', Value:'Yan'an'},
- {Name:'123', Value:'Hanzhong City'},
- {Name:'123', Value:'Yulin'},
- {Name:'123', Value:'Ankang City'},
- {Name:'123', 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.
1. 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:
- Var dataObj = eval ("("+ Data +")");// Convert 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 );
- })
Note: for General js json objects, you only need to replace the $. each () method with the for statement.
2. 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://www.phpzixue.cn /", {Param:"Gaoyusi"}, Function (data ){
- // The returned data is already a json object.
- // The following operations are the same as the first case
- $. Each (data. root, function (idx, item ){
- If(Idx =0){
- Return True;// Same as countinue, return false with break
- }
- Alert ("Name :"+ Item. name +", Value :"+ Item. value );
- });
- });
Note that the eval () method in method 1 dynamically executes the strings (which may be JavaScript scripts), which can easily cause system security problems. Therefore, some third-party client script libraries that circumvent eval () can be used. For example, JSON in JavaScript provides a script library of no more than 3 K.
- JQuery calls the WCF Service to pass JSON objects
- Several common methods required for learning jQuery
- Construct an ASP. NET Website using XML + XSLT + CSS + JQuery
- Use jQuery and PHP to build an Ajax-Driven Web Page
- Notes for calling WCF using jQuery