Home > Web Front-end > JS Tutorial > Detailed analysis of Jquery getJSON method_jquery

Detailed analysis of Jquery getJSON method_jquery

WBOY
Release: 2016-05-16 17:07:10
Original
1075 people have browsed it

Preparation
·Customer class

Copy code 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)
Copy code The code is as follows:

Customer customer = new Customer
{ Unid=1,CustomerName="Song Jiang",Memo="Tiankuixing",Other="Black Saburo"};
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, the processing function after the json data request is successful

Copy code 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
Copy code The code is as follows:

$.getJSON(
"webdata/Json_1.ashx",
function(data) {
$("#divmessage") .text(data.CustomerName);
}
);

Request json data from the Json_1.ashx address. After receiving the data, process the data data in the 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":"Tian Kuixing","Other":"Black Saburo"}

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

Copy code The code is as follows:

$.getJSON(
"webdata/Json_1.ashx",
function(data) {
var tt="";
                                                                                                                                                                    html(tt);
});


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


(2) Object array

Ashx file (Json_1.ashx) modification:

Copy code The code is as follows: List _list = new List();
Customer customer = new Customer
{ Unid=1, CustomerName="Song Jiang",Memo="Tian Kuixing",Other="Hei Sanlang"};
Customer customer2 = new Customer
{ Unid = 2, CustomerName = "Wu Yong", Memo = "Tianji Xing" , 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":"Song Jiang","Memo":"Tiankuixing","Other":"Herosaburo"},
{"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.

Copy code 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 of list json objects:

Copy code 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 "
";
      }); ").html(tt);
});


Result:
Unid: 1
CustomerName: Song Jiang
Memo: Tiankuixing
Other: Black Sanlang
Unid: 2
CustomerName: Wu Yong
Memo: Tianji Star
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, the k/v pair.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template