In fact, Json is a data format. The data is converted into a format in the background, and then parsed in the same method in the frontend, similar to serialization. The json format is mainly composed of key-value pairs, which can represent multiple data. For example,
{name:zhangsan,age:12,class:1}
At the same time, json can also represent a data set, which is composed of {} and:. For example, we need to query a table from the database and then transfer the table to the front desk, but the dataset cannot be transferred directly. We need to convert the dataset data into json data, which can facilitate the front desk js to parse the data. Let me write about the conversion below. The format
{Name: name of the table, Rows: [{SName: name, SAge: age}{...}{...}]} This is the data format of a table,
{Tables:[{Name: Name of Table 1, Rows: [{SName: Name, SAge: Age}{...}{...}]}{Name: Name of Table 2, Rows: [{SName: Name, SAge: Age}{...}{...}]}]} This is the data format of multiple tables
Let’s use an example to demonstrate 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 data in Json format. jquery nicely encapsulates such a function for us, JSON. parse();
We use jquery’s post function to get data from the background, and then parse the data. Now I will demonstrate the data format of the background
private String GetDataSet()
{
System.Data. DataSet ds = new System.Data.DataSet();
//Test data
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);
}
///
/// Convert dataTable to Json format
/// ///
///
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(""", "\"")); //For special characters, you should also undergo special treatment.
jsonBuilder.Append("",");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("},");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("]");
jsonBuilder.Append("}");
return jsonBuilder.ToString();
}
///
/// Convert DataSet to Json format
/// ///
DataSet
///
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 me show you the results
You can provide the corresponding format based on the data obtained
Don’t think that you are done here. The Json format will have compatibility issues in different browsers. In this case, you only need to download a js of json2.