Detailed analysis of the JquerygetJSON method _ jquery

Source: Internet
Author: User
This article mainly analyzes and introduces the JquerygetJSON method in detail. If you need a friend, please refer to it and hope to help you prepare for it.
· Customer class

The Code is as follows:


Public class Customer
{
Public int Unid {get; set ;}
Public string CustomerName {get; set ;}
Public string Memo {get; set ;}
Public string Other {get; set ;}
}


· Server processing (Json_1.ashx)

The Code is as follows:


Customer customer = new Customer
{Unid = 1, CustomerName = "", Memo = "Tian kuixing", Other = "sanlang "};
String strJson = Newtonsoft. Json. JsonConvert. SerializeObject (customer );

Context. Response. Write (strJson );


(1) Jquery. getJSON

Method definition: jQuery. getJSON (url, data, callback)

Get json data through get request
· Url is used to provide the address page of json data
· Data (Optional) is a key-value pair that is sent to the server.
· Callback (Optional) callback function, the processing function after the json data request is successful

The Code is as follows:


Function (data, textStatus ){
// Data is a json object
// TextStatus will be "success"
This; // the options for this ajax request
}


(1) An object

The Code is as follows:


$. GetJSON (
"Webdata/Json_1.ashx ",
Function (data ){
$ ("# Pmessage"). text (data. CustomerName );
}
);


Request json data from the Json_1.ashx address. After receiving the data, process the data in the function. The data here is a record, corresponding to a customer instance, where the data exists in k/V format. It exists as an array of [object, object.
{"Unid": 1, "CustomerName": "", "Memo": "", "Other": ""}

Therefore, data. Property is used for access. The following uses a k/v loop to print the song river record:

The Code is as follows:


$. GetJSON (
"Webdata/Json_1.ashx ",
Function (data ){
Var tt = "";
$. Each (data, function (k, v ){
Tt + = k + ":" + v +"
";
})
$ ("# Pmessage" 2.16.html (tt );
});


Result:
Unid: 1
CustomerName: Song Jiang
Memo: Tian kuixing
Other: Hei sanlang

(2) object Array
Modify the Ashx file (Json_1.ashx:

The Code is as follows:


List _ List = new List ();
Customer customer = new Customer
{Unid = 1, CustomerName = "", Memo = "Tian kuixing", Other = "sanlang "};
Customer customer2 = new Customer
{Unid = 2, CustomerName = "", Memo = "", Other = ""};

_ List. Add (customer );
_ List. Add (customer2 );
String strJson = Newtonsoft. Json. JsonConvert. SerializeObject (_ list );


The string of the json object it generates is:

[{"Unid": 1, "CustomerName": "", "Memo": "", "Other": ""},
{"Unid": 2, "CustomerName": "", "Memo": "", "Other": ""}]

Here we can see that the json object as a set is not another record, but two records. It is an array of [[object, object]: [object, object] [object, object], each [object, object] indicates a record, corresponding to a Customer. In fact, it is also in the form of k/v, and this v is a Customer object, this k is an index starting from 0.

The Code is as follows:


$. GetJSON (
"Webdata/Json_1.ashx ",
Function (data ){
$. Each (data, function (k, v ){
Alert (k );
});
});


At this time, the K value is 0, 1 ......

Method for listing json objects:

The Code is as follows:


$. GetJSON (
"Webdata/Json_1.ashx ",
Function (data ){
Var tt = "";
$. Each (data, function (k, v ){
$. Each (v, function (kk, vv ){
Tt + = kk + ":" + vv +"
";
});
});
$ ("# Pmessage" 2.16.html (tt );
});


Result:
Unid: 1
CustomerName: Song Jiang
Memo: Tian kuixing
Other: Hei sanlang
Unid: 2
CustomerName: Wu Yong
Memo: tianxingxing
Other: zhiduoxing

Nested loops are used here. The first loop is used to traverse the Customer object from the List, and the second loop is used to traverse the attributes of the Customer object from the Customer object, that is, k/v pairs.

Related Article

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.