It is convenient to add a reference DLL file to use. /201101/tools/newtonsoft.json.net20.rar
In the jquery library, Getjson is actually invoked: Query.get (URL, data, callback, "JSON")
Where the parameters are also emitted in k/v format. The request returned can be seen on the service side with the customer list collection returned
Now take a look at the list of things:
Pieces of a common class
Copy Code code 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 one of the following methods in the general processing file (ASHX)
Copy Code code as follows:
Customer customer = new Customer
{unid=1,customername= "Song Jiang", memo= "Days kuixing", other= "black Three Lang"};
String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (customer);
Context. Response.Write (Strjson);
}
Load the call ashx code in the foreground jquery
Request data to Ashx via Getjson. The returned data is a JSON object
Copy Code code 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"). HTML (TT);
});
});
(ii) ASHX file, but returns a collection of entities
Copy Code code as follows:
Customer customer = new Customer
{unid=1,customername= "Song Jiang", memo= "Days kuixing", other= "black Three Lang"};
Customer Customer2 = new Customer
{Unid = 2, CustomerName = "Wu use", Memo = "Secret Star", other = "Sundance"};
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"). HTML (TT);
});
}
[Code]
(iii) CS in the request ASPX file
[Code]
protected void Page_Load (object sender, EventArgs e)
{
Customer customer = new Customer
{Unid = 1, CustomerName = "Song Jiang", Memo = "Day Kuixing", other = "Black Three Lang"};
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 foreground file only retains the page declaration, all others are deleted
jquery Code
Copy Code code as follows:
function getcustomer_aspx () {
$.getjson (
"Webdata/json_1.aspx",
function (data) {
var tt = "";
$.each (data, function (k, v) {
tt + + k + ":" + V + "<br/>";
})
$ ("#divmessage"). HTML (TT);
});
}
(iv) Request for a text file
The text file provides the JSON string, and the $.getjson gets the JSON object
• Text File
{unid:1,customername: "Song Jiang", Memo: "Heaven Kuixing", Other: "Black Three Lang"}
The text file provides a JSON string that, for JSON, is ignored for this entity JSON, blank lines and spaces
Copy Code code as follows:
function Getcustomer_txt () {
$.getjson (
"Webdata/json_1.txt",
function (data) {
var tt = "";
$.each (data, function (k, v) {
tt + + k + ":" + V + "<br/>";
})
$ ("#divmessage"). HTML (TT);
});
}
The parsing method is the same as the ASHX resolution
The format for multiple lines in the TXT file is as follows:
Text content:
[
{unid:1,customername: "Song Jiang", Memo: "Heaven Kuixing", Other: "Black Three Lang"},
{unid:2,customername: "Wu uses", Memo: "Secret Star", Other: "Sundance"}
]
Copy Code code 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"). HTML (TT);
});
}
/201101/tools/newtonsoft.json.net20.rar