Just add the reference Dll file and you can use it, which is very convenient. /201101/tools/Newtonsoft.Json.Net20.rar
In the jquery library, getJSON is actually called: Query.get(url, data, callback, "json")
where Parameters are also sent in k/v pair format. You can see what the request returns: the server returns a Customer list collection
Now let’s look at the sequence of events:
Part 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; }
}
Write the following method in the general processing file (ashx)
Customer customer = new Customer
{ Unid=1 ,CustomerName="Song Jiang",Memo="Tian Kuixing",Other="Hei Sanlang"};
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
context.Response.Write(strJson) ;
}
Load the calling ashx code in the front-end Jquery
Request data from ashx through getJSON. 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 "
";
})
$("#disHows").html(tt);
});
});
(2) ashx file, but return is the entity collection
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 = "Tianjixing", Other = "Zhiduoxing" };
List _list = new List();
_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 "
";
});
});
$("#divmessage").html( tt);
});
}
[code]
(3) Request CS in aspx file
[code]
protected void Page_Load(object sender, EventArgs e)
{
Customer customer = new Customer
{ Unid = 1, CustomerName = "Song Jiang", Memo = "Tiankuixing", Other = "Black Saburo" };
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: Only keep the Page statement in the front-end file, and delete all others
Jquery code
function GetCustomer_Aspx() {
$.getJSON(
"webdata/Json_1. aspx",
function(data) {
var tt = "";
$.each(data, function(k, v) {
tt = k ":" v "
";
})
$("#divmessage").html(tt);
});
}
(4) Request text File
The text file provides a json string, and the json object is obtained from $.getJSON
·Text file
{Unid: 1, CustomerName: "Song Jiang", Memo: "Tian Kuixing", Other: "Hei Sanlang "}
The text file provides a json string. For the composition format of json, for this entity json, blank lines and spaces will be ignored
function GetCustomer_txt() {
$.getJSON(
"webdata/Json_1.txt",
function(data) {
var tt = "";
$.each(data, function(k, v) {
tt = k ":" v "
";
})
$("#divmessage").html(tt);
});
}
The parsing method is the same as that of ashx
The format for multiple lines in the Txt file is as follows:
Text content:
[
{Unid:1,CustomerName:"Song Jiang",Memo :"Tiankuixing",Other:"Herosaburo"},
{Unid:2,CustomerName:"Wu Yong",Memo:"Tianjixing",Other:"Zhiduoxing"}
]
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 "
";
});
});
$("#divmessage").html( tt);
});
}
/201101/tools/Newtonsoft.Json.Net20.rar