Preparation
·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="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
function(data, textStatus) {
// data is a json object
// textStatus will 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 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:
$.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:
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.
$.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:
$.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.