I have summarized some $. getjson () applications. I hope you will like it. First, I use newtonsoft. JSON. netw.dll.
You can use it by adding a reference DLL file.
In the jquery library, getjson is actually called: Query. Get (URL, Data, callback,
"JSON ")
The parameter is also in the K/V format. The returned request is displayed as follows: The returned result is from the list of customers on the server.
Now let's take a look:
A common class
Public Class Customer
{
Public int unid {Get; set ;}
Public String customername {Get; set ;}
Public String memo {Get; set ;}
Public String other {Get; set ;}
}
In the general processing file (ashx), write the following method: Customer customer = new customer
{Unid = 1, customername = "", memo = "Tian kuixing", other = "sanlang "};
String strjson = newtonsoft. JSON. jsonconvert. serializeobject (customer );
Context. response. Write (strjson );
}
Load and call ashx in jquery at the front end Code
Use getjson to request data from ashx. The returned data is a JSON object.
$ (). Ready (function (){
$. Getjson ("jquerydata2.ashx", function (data ){
Alert (data. Memo );
});
$. Getjson ("jquerydata2.ashx", function (data ){
VaR TT = "";
$. Each (data, function (K, v ){
Tt + = K + ":" + V + "<br/> ";
})
$ ("# Dishows" pai.html (TT );
});
});
(2) The ashx file, but the returned object set
Customer customer = new customer
{Unid = 1, customername = "", memo = "Tian kuixing", other = "sanlang "};
Customer customer2 = new customer
{Unid = 2, customername = "", memo = "", other = ""};
List <customer> _ list = new list <customer> ();
_ List. Add (customer );
_ List. Add (customer2 );
String strjson = newtonsoft. JSON. jsonconvert. serializeobject (_ list );
Context. response. Write (strjson );
}
Function getcustomerlist (){
$. Getjson (
"Jquerydata2.ashx ",
Function (data ){
VaR TT = "";
$. Each (data, function (K, v ){
$. Each (v, function (KK, vv ){
Tt + = KK + ":" + vv + "<br/> ";
});
});
$ ("# Divmessage" 2.16.html (TT );
});
}
(3) Request Cs in the aspx File
Protected void page_load (Object sender, eventargs E)
{
Customer customer = new customer
{Unid = 1, customername = "", memo = "Tian kuixing", other = "sanlang "};
String strjson = newtonsoft. JSON. jsonconvert. serializeobject (customer );
Response. Write (strjson );
}
· Aspx File
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "json_1.aspx.cs"
Inherits = "webdata_json_1" %>
Idea: the front-end file only retains the page declaration, and all others are deleted.
Jquery code function getcustomer_aspx (){
$. Getjson (
"Webdata/json_1.aspx ",
Function (data ){
VaR TT = "";
$. Each (data, function (K, v ){
Tt + = K + ":" + V + "<br/> ";
})
$ ("# Divmessage" 2.16.html (TT );
});
}
(4) Request text files
A text file provides a JSON string, which is a JSON object obtained by $. getjson.
· Text files
{Unid: 1, customername: "", Memo: "Tian kuixing", other: ""}
A text file provides a JSON string. For the JSON format, empty lines and spaces are ignored for this entity JSON.
Function getcustomer_txt () {$. getjson (
"Webdata/json_1.txt ",
Function (data ){
VaR TT = "";
$. Each (data, function (K, v ){
Tt + = K + ":" + V + "<br/> ";
})
$ ("# Divmessage" 2.16.html (TT );
});
}
The resolution method is the same as that of ashx.
The format of multiple rows in a TXT file is as follows:
Text Content:
[
{Unid: 1, customername: "", Memo: "Tian kuixing", other: "sanlang "},
{Unid: 2, customername: "", Memo: "Tianji Star", other: ""}
]
Function getcustomer_txtlist (){
$. Getjson (
"Webdata/json_1.txt ",
Function (data ){
VaR TT = "";
$. Each (data, function (K, v ){
$. Each (v, function (KK, vv ){
Tt + = KK + ":" + vv + "<br/> ";
});
});
$ ("# Divmessage" 2.16.html (TT );
});
} Http://s.click.taobao.com/t_8? E = 7hz5x % 2bozdswsvvyc5jts79au1q % 3d % 3d & P = mm_24156262_0_0