Home > Web Front-end > JS Tutorial > Code to get data set from background using jquery and json_jquery

Code to get data set from background using jquery and json_jquery

WBOY
Release: 2016-05-16 17:59:30
Original
1066 people have browsed it

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();

Copy code The code is as follows:



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
Copy code The code is as follows:

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template