A detailed analysis of Jquery Getjson method

Source: Internet
Author: User

  This article is mainly on the jquery Getjson method for detailed analysis of the introduction, the need for friends can come to the reference, I hope to help you.

Preparation Work · The Customer class   code is as follows: public class, customer {    public int Unid {get; set;}     public string Cus tomername {get; set;}     public string Memo {get; set;}     public string Other {get; set;}}   service-side processing (JSON_1.ASHX) code is as follows: Customer customer = new customer        {unid=1,customername= "Song Jiang", Me mo= "Day kuixing", 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 by getting Request URL Address page to provide JSON data data (Optional) The key value for delivery to the server callback (Optional) callback function, the JSON data request after the successful processing function   code as follows: function (data, textstatus) {      & nbsp Data is a JSON object        //Textstatus'll be "success"       The options for this AJAX request}   (1) An object code is as follows: $.getjson (    "Webdata/json_1.ashx",   &NBSP function (data) {       $ ("#divmessage"). Text (data. CustomerName);    });   Requests JSON data to the JSON_1.ASHX address, and then processes the data data in the function after receiving it. The data here is a record that corresponds to a customer instance in which the data exists in k/v form. That is, in the form of a [Object,object] array. {"Unid": 1, "CustomerName": "Song Jiang", "Memo": "Days Kuixing", "other": "Black Tri Lang"}   so in the access to data. property to access, the following k/v loop to print the record of Song Jiang:   code as follows: $.getjson (    "Webdata/json_1.ashx",     function ( Data) {        var tt= "";       $.each   (data, function (k, v) {    &NB Sp       TT = k + ":" + V + "<br/>";        })         $ ("#divmessage"). HTML (TT); });   Results: unid:1 CustomerName: Song Jiang Memo: Days kuixing Other: Haisanlong   (2) Object array ashx file (json_1.ashx) Modified:     Code as follows: List<cus tomer> _list = new list<customer> ();  Customer customer = new customer         {Un Id=1,customername= "Song Jiang", memo= "Day kuixing", other= "black Three Lang"}; CustomeR customer2 = new customer         {Unid = 2, CustomerName = "WU", Memo = "Secret Star", other = "Sundance"} ;   _list. ADD (customer); _list. ADD (CUSTOMER2); String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (_list);      The strings of the Json object it generates are:   [{] Unid ": 1," CustomerName ":" Song Jiang "," Memo ":" Days Kuixing "," other ":" Black Three Lang "}, {" Unid ": 2," CustomerName ":" Wu Use "," Memo ":" Secret Star "," Other ":" Sundance "}]   Here you can see that the JSON object as a collection is not another record, but 2 records, a [[Object,object]] array: [Object,object][object,object], and each [object, Object] Represents a record, which corresponds to a customer, which is also a k/v form, and this v is a customer object, and this k is an index starting from 0.   Code as follows: $.getjson (    "Webdata/json_1.ashx",     function (data) {        $. Each (data, function (k, v) {            alert (k);      - }); });   At this point, K value is 0,1 ...   list Json object method:   Code as follows: $.getjson (    "Webdata/json_1.ashx",     function (data) {        var tt = "";   &NBsp     $.each (data, function (k, v.) {            $.each (v,function (KK, vv) {  & nbsp             TT + KK + ":" + vv + "<br/>";            });        });         $ ("#divmessage"). HTML (TT); });   Results: unid:1 CustomerName: Song Jiang Memo: Days kuixing Other: Haisanlong unid:2 CustomerName: Wu with Memo: Secret star Other: Sundance     Here used a nested loop, the first to follow The 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.  

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.