Has recently been plagued by jquery, a demo of jquery parsing json, This demo wants to implement a DataSet or DataTable dataset from the ASP.net background, convert the dataset to JSON and return it to the client, which is parsed by the jquery Getjson method and displayed on the page.
First, briefly introduce the Getjson method
Jquery.getjson (Url,[data],[callback])
URL: Sends the request address.
Data: Key/value parameter to be sent.
Callback: callback function when load succeeds.
Here is the actual combat Getjson method
First, you create a secondary class that converts the dataset DataSet to a JSON string
Copy Code code as follows:
public static string Datatabletojson (String jsonname, DataTable DT)
{
StringBuilder Json = new StringBuilder ();
Json.append ("{\" "+ Jsonname +" \ ": [");
if (dt. Rows.Count > 0)
{
for (int i = 0; i < dt. Rows.Count; i++)
{
Json.append ("{");
for (int j = 0; j < dt.) Columns.count; J + +)
{
Json.append ("\" "+ dt.) COLUMNS[J]. Columnname.tostring () + "\": \ "" + dt. ROWS[I][J]. ToString () + "\" ");
if (J < dt. COLUMNS.COUNT-1)
{
Json.append (",");
}
}
Json.append ("}");
if (i < dt. ROWS.COUNT-1)
{
Json.append (",");
}
}
}
Json.append ("]}");
return json.tostring ();
}
This method is a helper class method on MSDN.
The second step is to manually configure the creation of a demo dataset, but in a project, you typically get data from a database or service
Copy Code code as follows:
public static DataSet Binddata ()
{
DataTable dtdata = new DataTable ();
DTDATA.COLUMNS.ADD ("id");
DTDATA.COLUMNS.ADD ("name");
DTDATA.COLUMNS.ADD ("Sex");
DataRow Drdata;
Drdata = Dtdata.newrow ();
Drdata[0] = 16;
DRDATA[1] = "Zhaoliu";
DRDATA[2] = "man";
DTDATA.ROWS.ADD (Drdata);
Drdata = Dtdata.newrow ();
Drdata[0] = 19;
DRDATA[1] = "Zhangsan";
DRDATA[2] = "women";
DTDATA.ROWS.ADD (Drdata);
DataSet ds = new DataSet ();
Ds. Tables.add (Dtdata);
return DS;
}
Step three create an ASPX page
Front page: Two button, one click to parse JSON data, and another to view the JSON string
Copy Code code as follows:
<title></title>
<script language= "javascript" type= "Text/javascript" src= "Scripts/jquery-1.4.1.min.js" ></script>
<script language= "javascript" type= "Text/javascript" >
$ (function () {
$ ("#btn"). Click (function () {
$.getjson ("Getjsondemo.aspx", {action: "Action"},
function (data) {
var txt = "";
$.each (data, function (k, v) {$.each (V, function (m, n) {txt = = "ID:" +n.id + "; Name:" + n.name + "; Sex:" +n.sex+ "<b R/> "}); });
$ ("#txt"). HTML (TXT);
});
});
});
$ (function () {
$ ("#btn2"). Click (function () {
$.get ("Getjsondemo.aspx", {action: "Action"},
function (data) {$ ("#txt2"). Text (data); });
});
});
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<input id= "btn" type= "button" value= "Paser json"/><br/>
<input id= "btn2" type= "button" value= "Watch json string"/><br/>
<label id= "txt" ></label><br/>
<label id= "Txt2" ></label>
</div>
</form>
</body>
Background page:
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
Jsonajax ();
}
private void Jsonajax () {
String action = request["Action"];
if (!string. IsNullOrEmpty (action) && action = "Action")//To determine if a click event in the foreground
{
String str = Datatableconvertjson.datatabletojson ("JSON", Data.binddata (). Tables[0]);
Response.Write (str);
Response.End ();
}
}
Finally, let's show you the generated JSON format:
Top of Form
{"JSON": [{"id": "" "," Name ":" Zhaoliu "," Sex ":" Man "},{" id ":" "," Name ":" Zhangsan "," Sex ":" Women "}]}
Bottomof Form