jquery parsing JSON and JSON examples

Source: Internet
Author: User
Tags eval

How jquery handles Web effects on

jquery's way of processing JSON is to directly eval, to a variable, but today, seeing its source code, proves that my thoughts are completely wrong.


It first determines if there is no window.json.parse this method, if there is, directly with this method, if not, will be a new anonymous function, the contents, is to return the results of JSON.


I tried, window.json this object, in Firefox/webkit's browser, but Ie6-ie8 none.


In addition, if you need to parse the JSON string, you can use the Parsejson method of jquery directly, although this method does not appear in the manual, for example:


var json_str = ' {' A ': 1, "B": 2} ';
var data = $.parsejson (JSON_STR);
Alert (DATA.A)


A more detailed method and principle of JSON processing

jquery asynchronously gets the data type--web effects on objects and strings based on the results of two ways to get the result of processing.
1. For the Web page effects returned by the server on the string, if the jquery asynchronous request does not do type description, or as a string acceptance, then need to do an object processing, not too cumbersome, or put the string in eval () to execute once. This is also a good way to get JSON objects in a normal Javascipt way, as illustrated in the following example:

var dataobj=eval ("(" +data+ ")");//Convert to JSON object
Alert (dataobj.root.length)//output The number of child objects of root
$.each (Dataobj.root,fucntion (Idx,item) {
if (idx==0) {
return true;
}


Output the name and value of each root child object

alert ("Name: +item.name+", Value: "+item.value");
})


Note: For the general JS generation JSON object, only need to replace the $.each () method with the For statement, the other unchanged.
2. For the JSON string returned by the server, the eval () method is not required if the jquery asynchronous request sets the type (typically this configuration property) to "JSON" or uses the $.getjson () method to get the server back. Because the result is already a JSON object, just call the object directly, and here take the $.getjson method as an example to illustrate 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 other actions are the same as the first case
$.each (Data.root,function (Idx,item) {
if (idx==0) {
Return true;//with Countinue, returns false with break
}
Alert ("Name: +item.name+", Value: "+item.value");
});
});

jquery Processing JSON data instances

<title>jquery Get JSON Data demo page </title>
<script type= "text/web Effects" src= "Js/jquery-1.2.1.pack.js" ></script>
<script type= "Text/javascript" >
function GetData () {
$ ("#list"). HTML ("");//Empty the data in the list
Send an AJAX request
$.getjson (
"Jsondata.ashx",//server-side page generating JSON data
{name: ' Test ', age:20},//the query string sent to the server (optional for this parameter)
Handles the returned JSON data, which is presented as a list
function (JSON) {
Loop through the data in JSON and present it in the list
$.each (Json,function (i) {
$ ("#list"). Append ("<li>name: +json[i].name+" &nbsp; Age: "+json[i].age+" </li>)
})
})
}
</script>


ASP tutorial. Net

: <%@ WebHandler language= "C #" class= "Jsondata"%>

using System;
Using System.Web;

public class Jsondata:ihttphandler {

public void ProcessRequest (HttpContext context) {
Context.response.contenttype = "Text/plain";
String data = "[{name:\" ants\ ", age:24},{name:\" lele\ ", age:23}]";//built JSON data
The following two sentences are used to test the query characters that the foreground sends to this page
String querystrname = Context.request.querystring.getvalues ("name") [0];//the value Namer in the query string
String querystage = Context.request.querystring.getvalues ("Age") [0];//Fetch the value of age in the query string

Context.Response.Write (data);
}

public bool IsReusable {
get {
return false;
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.