In fact, JSON is the format of data, in the background to the data block into a format, and then in the foreground with the same method parsing, similar to serialization. The JSON format consists primarily of key-value pairs, which can represent multiple data. Like what
{name:zhangsan,age:12,class:1}
JSON can also represent a dataset, which is also {} and: to compose. For example, we need to query a table from the database, and then transfer the table to the foreground, but the dataset is not directly transmitted, we need to convert the dataset data to JSON data, so that you can facilitate the foreground JS parsing data, I will write the format of the conversion
{Name: The name of the table, Rows:[{sname: Name, SAge: Age} {...} {...}]} This is a tabular data format,
{tables:[{name: Table 1, Rows:[{sname: Name, SAge: Age} {...} {...}]} {Name: Name of Table 2, Rows:[{sname: Name, SAge: Age} {...} {...}]}]} This is the data format for more than one table
Here is an example to show you the data set transmission
First we need a front page to get the data studentinfo.html, in this page we have a function to get the JSON format data, jquery nicely encapsulates a function like this, json.parse ();
Copy Code code as follows:
<script type= "Text/javascript" >
function GetData () {
$ ("#data"). Val ("");
Jquery.post ('.. /ashx_for_request/getdataset.ashx ', {}, function (data) {//data for background transmissions
var obj = json.parse (data); Convert the data that is transferred to JSON format
$ ("#data"). HTML ("");
$.each (obj. tables, function (index, table) {//Traverse DataSet table, output data set contents
Displays different fields, depending on the name of the table. Get a specific table, table = obj. Tables[0]
var tablename = table. Name;
$.each (table. Rows, function (index, ROW) {//Traverse a row in the dataset table
$ ("#data"). HTML ($ ("#data"). html () + row. SID + row. Sname + row. SAge + row. Sclass + row. Ssex + row. Sgrade + "</br>"); The contents of each column in each row here we use Row.colname to get the contents of each column of each row
});
});
})
}
</script>
We're using the jquery post function to get the data from the background and then parse the data, and now I'm going to demonstrate the background data format
Copy Code code as follows:
Private String GetDataSet ()
{
System.Data.DataSet ds = new System.Data.DataSet ();
Data for testing
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection ("server=.; database=student;uid=sa;pwd=123456 "))
{
using (System.Data.SqlClient.SqlCommand com=conn. CreateCommand ())
{
Com.commandtext = "SELECT * from Basenews";
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter (COM);
Da. Fill (DS);
}
}
Return Dataset2json (DS);
}
<summary>
DataTable converted to JSON format
</summary>
<param name= "DT" ></param>
<returns></returns>
public static string Datatable2json (System.Data.DataTable dt)
{
StringBuilder Jsonbuilder = new StringBuilder ();
Jsonbuilder.append ("{\" name\ ": \" "+ dt.) TableName + "\", \ "Rows");
Jsonbuilder.append ("\": [");
for (int i = 0; i < dt. Rows.Count; i++)
{
Jsonbuilder.append ("{");
for (int j = 0; j < dt.) Columns.count; J + +)
{
Jsonbuilder.append ("\");
Jsonbuilder.append (dt. COLUMNS[J]. ColumnName);
Jsonbuilder.append ("\": \ "");
Jsonbuilder.append (dt. ROWS[I][J]. ToString (). Replace ("\", "\\\")); Special processing should also be done for special characters.
Jsonbuilder.append ("\", ");
}
Jsonbuilder.remove (jsonbuilder.length-1, 1);
Jsonbuilder.append ("},");
}
Jsonbuilder.remove (jsonbuilder.length-1, 1);
Jsonbuilder.append ("]");
Jsonbuilder.append ("}");
return jsonbuilder.tostring ();
}
<summary>
DataSet converted to JSON format
</summary>
<param name= "DS" >DataSet</param>
<returns></returns>
public static string Dataset2json (System.Data.DataSet ds)
{
StringBuilder json = new StringBuilder ();
Json. Append ("{\" tables\ ":");
Json. Append ("[");
foreach (System.Data.DataTable dt in DS. Tables)
{
Json. Append (Datatable2json (DT));
Json. Append (",");
}
Json. Remove (JSON. Length-1, 1);
Json. Append ("]");
Json. Append ("}");
Return JSON. ToString ();
}
Let's see the results.
You can give the appropriate format based on the data you get.
We don't think it's done here. Oh, JSON format in different browsers will have compatibility issues, then we just download a Json2 js can.