Home  >  Article  >  Web Front-end  >  Detailed analysis of Jquery getJSON method usage

Detailed analysis of Jquery getJSON method usage

巴扎黑
巴扎黑Original
2017-07-03 09:28:251088browse

This article mainly provides a detailed analysis and introduction to the Jquery getJSON method. Friends in need can come and refer to it. I hope it will be helpful to everyone.

Preparation work
·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-side processing (Json_1.ashx )

The code is as follows:

Customer customer = new Customer
{ Unid=1,CustomerName="Song Jiang",Memo="Tian Kuixing",Other="Hei 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 used to transmit the key-value pair to the server
·callback(Optional)Callback function, json data request is successful The final processing function

code is as follows:

function(data, textStatus) {
// data is a json object
           // textStatus will be "success"
                                                                                                                                                                                                        

##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 data in function. The data here is a record, corresponding to a customer instance, and the data in it exists in k/v form. That is, it exists in the form of [object, object] array.
{"Unid":1,"CustomerName":"Song Jiang","Memo":"Tiankuixing","Other":"Herosaburo"}

So when accessing, use data. Property to access, the following uses k/v loop to print this record of Song Jiang:

The code is as follows:


$.getJSON(
"webdata/Json_1.ashx" ,

function(data) {

var tt="";
$.each(data, function(k, v) {

tt += k + ":" + v + "< ;br/>";

})

 $("#pmessage").html(tt);

});



Result:
Unid: 1
CustomerName: Song Jiang
Memo: Tiankuixing
Other: Black Saburo


(2) Object Array


Ashx file (Json_1.ashx) modification:

The code is as follows:List _list = new List();
Customer customer = new Customer
{unid = 1, Customername = "Song Jiang", memo = "Tiankui Xing", Other = "Hexanlang"};
Customer Customer2 = New Customer
{UNID = 2, CUSTOMERNAME = " , Memo = "Tianjixing", Other = "Zhiduoxing" };

_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" :"Song Jiang","Memo":"Tiankuixing","Other":"黑三郎"},
{"Unid":2,"CustomerName":"Wu Yong","Memo":"Tianjixing ","Other":"Zhiduoxing"}]

Here you can see that the json object as a collection is not one record, but 2 records. It is an array of [[object, object]]: [object, object] [object, object], and each [ object, object] represents a record, corresponding to a Customer, which is actually in the form of k/v, and this v is a Customer object, and this k is the 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...

Methods for listing json objects:

The code is as follows:

$.getJSON(
"webdata/Json_1.ashx",
function( data) {
var tt = "";
                                                             kk + ":" + vv + "
";
                                                                   ;



Result:
Unid: 1
CustomerName: Song Jiang

Memo: Tiankuixing

Other: Black Saburo
Unid: 2
CustomerName: Wu Yong
Memo: Tianjixing
Other: Zhiduoxing


A nested loop is used here. The first loop is used to traverse the Customer object from the List, and the second loop is used to traverse the Customer object from the List. Used to traverse the
properties
of the Customer object from the Customer object, that is, k/v pairs.

The above is the detailed content of Detailed analysis of Jquery getJSON method usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn