How to use JSON in front of the background data transfer examples _javascript tips

Source: Internet
Author: User
Tags goto string back
The previous blog wrote that using JavaScript to generate multiple sets of text, so that the input of the data is not displayed, now we need to write these input to the database, where the JSON passed in.

First, let's write the background on how to generate the data to be transferred
[HTML]
Copy Code code as follows:

function Generatedtb () {
Write
var txtname = document.getElementById ("Txtname"). Value;
Create an array
var dtb = new Array ();
Writes data to an array by looping and returns
for (var i = 0; i < firstgroup.length; i++) {
var row = new Object ();
Row. Name = txtname;
Row.fullmoney = Firstgroup[i].value;
Row.discount = Secondgroup[i].value;
Dtb.push (row);
}
return DTB;
}
function Generatedtb () {
Write
var txtname = document.getElementById ("Txtname"). Value;
Create an array
var dtb = new Array ();
Writes data to an array by looping and returns
for (var i = 0; i < firstgroup.length; i++) {
var row = new Object ();
Row. Name = txtname;
Row.fullmoney = Firstgroup[i].value;
Row.discount = Secondgroup[i].value;
Dtb.push (row);
}
return DTB;
}

Converts an array into a JSON string for incoming to the background:
[HTML]
Copy Code code as follows:

$ (function () {
Click Botton1
$ ("#lbtnOK"). Click (function () {
var url = "Discountmanger.aspx?ajax=1";
var dtb = GENERATEDTB ();
var strName = document.getElementById ("Txtname"). Value;
if (DTB = null)
{ }
else {
Serializing an Object
var postdata = json.stringify (DTB);
Asynchronous request
$.post (URL, {json:postdata}, function (JSON) {
if (JSON) {
Jbox.tip ("Add success!") "," hint ");
Location.reload ();
}
else {
Jbox.tip ("Add failed!") "," hint ");
Location.reload ();
}
}, "JSON")
}
});
});
$ (function () {
Click Botton1
$ ("#lbtnOK"). Click (function () {
var url = "Discountmanger.aspx?ajax=1";
var dtb = GENERATEDTB ();
var strName = document.getElementById ("Txtname"). Value;
if (DTB = null)
{ }
else {
Serializing an Object
var postdata = json.stringify (DTB);
Asynchronous request
$.post (URL, {json:postdata}, function (JSON) {
if (JSON) {
Jbox.tip ("Add success!") "," hint ");
Location.reload ();
}
else {
Jbox.tip ("Add failed!") "," hint ");
Location.reload ();
}
}, "JSON")
}
});
});

Actions in the background:
First, determine if you need to transfer data
[HTML]
Copy Code code as follows:

if (! IsPostBack)
{
To determine whether an asynchronous request
if (request.querystring["ajax"] = = "1")
{
ProcessRequest ();
}
if (! IsPostBack)
{
To determine whether an asynchronous request
if (request.querystring["ajax"] = = "1")
{
ProcessRequest ();
}

Here's what you do with the data:
[HTML]
Copy Code code as follows:

<summary>
Processing asynchronous requests
</summary>
private void ProcessRequest ()
{
Depositing the policy to be filled in
ArrayList arrdiscount = new ArrayList ();
Response.ContentType = "text/html";
String json = request.form["JSON"];
Deserializing a DataTable
if (JSON = = null)
{
Return
}
Else
{
DataTable NEWDTB = JSON2DTB (JSON);
for (int i = 0; i < NEWDTB. Rows.Count; i++)
{
Entity.strategydiscount enstrategydiscount = new Entity.strategydiscount ();
Discount Scheme name
Enstrategydiscount.name = Newdtb. rows[i]["Name"]. ToString ();
Store ID
Enstrategydiscount.shopid = long. Parse (LOGININFO.SHOPID);
Enstrategydiscount.fullmoney = Convert.todecimal (NEWDTB. rows[i]["Fullmoney"]. ToString ());
Enstrategydiscount.discount = Convert.todecimal (NEWDTB. rows[i]["Discount"]. ToString ());
Writing data to an array
Arrdiscount.add (Enstrategydiscount);
}
Writing data to a database
ISTRATEGYBLL strategy = new STRATEGYBLL ();
if (strategy. Addstrategydiscount (Arrdiscount))
{
Response.Write ("true");
Response.End ();
}
Else
{
Response.Write ("false");
Response.End ();
}
}
<summary>
Processing asynchronous requests
</summary>
private void ProcessRequest ()
{
Depositing the policy to be filled in
ArrayList arrdiscount = new ArrayList ();
Response.ContentType = "text/html";
String json = request.form["JSON"];
Deserializing a DataTable
if (JSON = = null)
{
Return
}
Else
{
DataTable NEWDTB = JSON2DTB (JSON);
for (int i = 0; i < NEWDTB. Rows.Count; i++)
{
Entity.strategydiscount enstrategydiscount = new Entity.strategydiscount ();
Discount Scheme name
Enstrategydiscount.name = Newdtb. rows[i]["Name"]. ToString ();
Store ID
Enstrategydiscount.shopid = long. Parse (LOGININFO.SHOPID);
Enstrategydiscount.fullmoney = Convert.todecimal (NEWDTB. rows[i]["Fullmoney"]. ToString ());
Enstrategydiscount.discount = Convert.todecimal (NEWDTB. rows[i]["Discount"]. ToString ());
Writing data to an array
Arrdiscount.add (Enstrategydiscount);
}
Writing data to a database
ISTRATEGYBLL strategy = new STRATEGYBLL ();
if (strategy. Addstrategydiscount (Arrdiscount))
{
Response.Write ("true");
Response.End ();
}
Else
{
Response.Write ("false");
Response.End ();
}
}

Here, we need to convert JSON into a DataTable.
[HTML]
Copy Code code as follows:

<summary>
JSON Goto DataTable
</summary>
<param name= "JSON" ></param>
<returns></returns>
Private DataTable JSON2DTB (string json)
{
JavaScriptSerializer JSS = new JavaScriptSerializer ();
ArrayList dic = JSS. Deserialize<arraylist> (JSON);
DataTable DTB = new DataTable ();
if (DIC). Count > 0)
{
foreach (dictionary<string, object> drow in dic)
{
if (DTB. Columns.count = 0)
{
foreach (String key in Drow. Keys)
{
DtB. Columns.Add (Key, Drow[key]. GetType ());
}
}
DataRow row = DtB. NewRow ();
foreach (String key in Drow. Keys)
{
Row[key] = Drow[key];
}
DtB. Rows.Add (row);
}
}
return DTB;
}
<summary>
JSON Goto DataTable
</summary>
<param name= "JSON" ></param>
<returns></returns>
Private DataTable JSON2DTB (string json)
{
JavaScriptSerializer JSS = new JavaScriptSerializer ();
ArrayList dic = JSS. Deserialize<arraylist> (JSON);
DataTable DTB = new DataTable ();
if (DIC). Count > 0)
{
foreach (dictionary<string, object> drow in dic)
{
if (DTB. Columns.count = 0)
{
foreach (String key in Drow. Keys)
{
DtB. Columns.Add (Key, Drow[key]. GetType ());
}
}
DataRow row = DtB. NewRow ();
foreach (String key in Drow. Keys)
{
Row[key] = Drow[key];
}
DtB. Rows.Add (row);
}
}
return DTB;
}

This allows the data to be written to the database without flushing.
Of course, if we have a DataTable read from the database, if it's shown in the foreground by JSON.
First, we need to convert the DataTable to JSON data
[HTML]
Copy Code code as follows:

<summary>
DataTable Turn JSON
</summary>
<param name= "DTB" ></param>
<returns></returns>
private String Dtb2json (DataTable dtb)
{
JavaScriptSerializer JSS = new JavaScriptSerializer ();
ArrayList dic = new ArrayList ();
foreach (DataRow row in DTB. Rows)
{
Dictionary<string, object> drow = new dictionary<string, object> ();
foreach (DataColumn col in DtB. Columns)
{
Drow. Add (Col. ColumnName, Row[col. ColumnName]);
}
Dic. ADD (drow);
}
Return JSS. Serialize (DIC);
}
<summary>
DataTable Turn JSON
</summary>
<param name= "DTB" ></param>
<returns></returns>
private String Dtb2json (DataTable dtb)
{
JavaScriptSerializer JSS = new JavaScriptSerializer ();
ArrayList dic = new ArrayList ();
foreach (DataRow row in DTB. Rows)
{
Dictionary<string, object> drow = new dictionary<string, object> ();
foreach (DataColumn col in DtB. Columns)
{
Drow. Add (Col. ColumnName, Row[col. ColumnName]);
}
Dic. ADD (drow);
}
Return JSS. Serialize (DIC);
}

and write back to the front desk.
[HTML]
Copy Code code as follows:

<summary>
Processing asynchronous requests
</summary>
private void ProcessRequest ()
{
Response.ContentType = "text/html";
String json = request.form["JSON"];
Deserializing a DataTable
DataTable NEWDTB = JSON2DTB (JSON);
Serializing a DataTable as JSON
String back = Dtb2json (NEWDTB);
Response.Write (back);
Response.End ();
}
<summary>
Processing asynchronous requests
</summary>
private void ProcessRequest ()
{
Response.ContentType = "text/html";
String json = request.form["JSON"];
Deserializing a DataTable
DataTable NEWDTB = JSON2DTB (JSON);
Serializing a DataTable as JSON
String back = Dtb2json (NEWDTB);
Response.Write (back);
Response.End ();
}

Receive display at foreground:
[HTML]
Copy Code code as follows:

$ (function () {
Click Botton1
$ ("#botton1"). Click (function () {
CreateTable (JSON);
});
});
Displaying data in JSON
function CreateTable (JSON) {
var table = $ ("<table border= ' 1 ' ></table>");
for (var i = 0; i < json.length; i++) {
O1 = Json[i];
var row = $ ("<tr></tr>");
For (key in O1) {
var td = $ ("<td></td>");
Td.text (O1[key].tostring ());
Td.appendto (row);
}
Row.appendto (table);
}
Table.appendto ($ ("#back"));
}
$ (function () {
Click Botton1
$ ("#botton1"). Click (function () {
CreateTable (JSON);
});
});
Displaying data in JSON
function CreateTable (JSON) {
var table = $ ("<table border= ' 1 ' ></table>");
for (var i = 0; i < json.length; i++) {
O1 = Json[i];
var row = $ ("<tr></tr>");
For (key in O1) {
var td = $ ("<td></td>");
Td.text (O1[key].tostring ());
Td.appendto (row);
}
Row.appendto (table);
}
Table.appendto ($ ("#back"));
}

This completes the JSON to the background data transfer and display the background data, of course, this transmission is only one of the transmission, if it is a simple string can be used to transmit with get and post, but JavaScript itself has unsafe and unstable rows, for some of the more important data, Suggest or find some more reliable methods.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.