You can use it by adding a reference Dll file. /201101/tools/newtonsoft.json.netw.rar
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
Copy codeThe 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 ;}
}
Write the following method in the general processing file (ashx ):Copy codeThe Code is as follows: 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 the ashx code in Jquery at the front end
Use getJSON to request data from ashx. The returned data is a JSON object.Copy codeThe Code is as follows: $ (). 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 setCopy codeThe Code is as follows: 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 );
});
}
[Code]
(3) Request cs in the aspx File
[Code]
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 codeCopy codeThe Code is as follows: 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.Copy codeThe Code is as follows: 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: ""}
]Copy codeThe Code is as follows: 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 );
});
}
/201101/tools/newtonsoft.json.netw.rar