Analysis of Jquery Getjson method (I.)

Source: Internet
Author: User

Preparatory work

· Customer class

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

• Server-side processing (JSON_1.ASHX)

Customer customer = new Customer
{unid=1,customername= "Song Jiang", memo= "Tian Kui xing", other= "black Three Lang"};
String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (customer);

Context. Response.Write (Strjson);

(i) Jquery. Getjson

Method definition: Jquery.getjson (URL, data, callback)

Get JSON data with GET request

URL The address page used to provide JSON data

data (Optional) for transferring key-value pairs to the server

Callback (Optional) callback function, the processing function after the JSON data request succeeds

function (data, textstatus) {
Data is a JSON object
Textstatus'll be "success"
This The options for this AJAX request
}

(1) An object

$.getjson (
"Webdata/json_1.ashx",
function (data) {
$ ("#divmessage"). Text (data. CustomerName);
}
);

Request JSON data to the JSON_1.ASHX address, and after receiving the data, process the data in function. Data here is a record that corresponds to a customer instance where the data exists in k/v form. That exists as an array of [Object,object].

{"Unid": 1, "CustomerName": "Song Jiang", "Memo": "Tian Kui xing", "other": "Black Three Lang"}

So when visiting, take data. property to access, the following is a k/v loop to print this song song:

$.getjson (
"Webdata/json_1.ashx",
function (data) {
var tt= "";
$.each (data, function (k, v) {
tt + = k + ":" + V + "<br/>";
})
$ ("#divmessage"). HTML (TT);
});

Results:

Unid:1
CustomerName: Song Jiang
Memo: Tian Kui xing
Other: Black Tri lang

(2) Object array

ashx file (json_1.ashx) Modified:

list<customer> _list = new list<customer> ();
Customer customer = new Customer
{unid=1,customername= "Song Jiang", memo= "Tian Kui xing", other= "black Three Lang"};
Customer Customer2 = new Customer
{Unid = 2, CustomerName = "WU", Memo = "Secret Star", other = "mastermind"};

_list. ADD (customer);
_list. ADD (CUSTOMER2);
String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (_list);

The string that it generates for the JSON object is:

[{"Unid": 1, "CustomerName": "Song Jiang", "Memo": "Tian Kui xing", "other": "Black Three Lang"},

{"Unid": 2, "CustomerName": "WU", "Memo": "The Secret Star", "Other": "Mastermind"}]

Here you can see that the JSON object as a collection is not another record, but 2 records, is a [[Object,object]] array: [Object,object][object,object], and each [Object,object] represents a record , which corresponds to a customer, is actually a form of k/v, and this v is a customer object, and this k is the index starting at 0.

$.getjson (
"Webdata/json_1.ashx",
function (data) {
$.each (data, function (k, v) {
Alert (k);
});
});

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

Methods for listing JSON objects:

$.getjson (
"Webdata/json_1.ashx",
function (data) {
var tt = "";
$.each (data, function (k, v) {
$.each (V,function (KK, VV) {
tt + = KK + ":" + vv + "<br/>";
});
});
$ ("#divmessage"). HTML (TT);
});

Results:

Unid:1
CustomerName: Song Jiang
Memo: Tian Kui xing
Other: Black Tri lang
Unid:2
CustomerName: Wu Yong
Memo: The Secret star
Other: Mastermind

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 properties of the customer object from the customer object, that is, the k/v pair.

For serialization and deserialization see Other Essays (JSON)

Analysis of Jquery Getjson method (I.)

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.