Analysis of Jquery Getjson method

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/jams742003/archive/2009/12/25/1632276.html

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= "Day Kui xing", other= "black three lang"};string Strjson = NEWTONSOFT.J Son. 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 F or 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: Day Kui xing Other: black three 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= "Day 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 + "&                  Lt;br/> ";           });         }); $ ("#divmessage"). HTML (TT); });

Results:

Unid:1 CustomerName: Song Jiang Memo: Day Kui xing Other: Haisanlong unid:2 CustomerName: Wu with 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)

Blog Park Avenue to Jane

http://www.cnblogs.com/jams742003/

Reprint Please specify: Blog Park

Analysis of Jquery Getjson method

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.