Chaotic JSON, JS object, and JS Array

Source: Internet
Author: User

I have never really learned about JSON. I only heard about it when I used extjs for my company OA. Yesterday, I encountered a problem that I had never encountered before, that is, how to parse the JSON string uploaded to the background at the front end. Previously, data in the background was converted into a JSON string and transferred to the foreground for analysis by extjs itself. This is different. The order is just the opposite. Because it uses. NET 2.0, there is no legendary JSON parsing class that comes with 3.5, So I downloaded newstonsoft JSON. Net online. After the DLL is introduced, the expected results are not displayed for a long time. The JSON string returned by the front-end is:

[{"ID": "123", "home": "256", "Temp", "240" },{ "ID": "254", "home ": "600" },{ "ID": "1234", temp "," 240 "}]

What I want in the background is to parse them into the following SQL statements:

Update tableexample set home = '000000', temp = '000000' where id = '000000'
Update tableexample set home = '000000' where id = '000000'
Update tableexample set temp = '000000' where id = '000000'

To be honest, I don't know if the above string is not a JSON string, because I have also seen {[{"ID": "123" },{ "ID ": "123" },{ "ID": "2222. But I know that the JSON string is actually an array in JS, and the content of this array is three JS objects:

Let's look at how to define array and object in Js.

Define array.

VaR vararray = new array ();

Or assign values directly.

VaR vararray = new arrau (["apple", "Pear", "orange"]);

Define object

VaR varobject = new object ();

Or assign values directly.

VaR varobject = new object ({"ID": "123", "home": "256", "Temp", "240 "});

 

In this case, the JSON string I just created can also be assigned to an array as it complies with the array format-enclosed in [], and the elements in it are separated by characters, isn't it?

VaR vararrau = new array ([{"ID": "123", "home": "256", "Temp", "240" },{ "ID ": "254", "home": "600" },{ "ID": "1234", temp "," 240 "}])

Here we can see that the content in the array in JS can be anything, including objects.

Then we infer that JSON is actually a mix of array and object in JS format. So we need to solve the actual problem here. Should we convert the string into the SQL statement I want? Use regular expressions? Split split?

My God. It is a headache. As a matter of fact, we have just analyzed that the JSON string is actually an array, and three objects are put in the array. So why don't we just use js to traverse this array, retrieve the objects in it, traverse the objects in it, and retrieve the attributes and values of the objects? The JS script is as follows:

VaR pagedto = new string ();
VaR varchangeid = new array ();
Pagedto = pagedto. Concat ("[");
For (VAR I = 0; I <updatedate. length; I ++ ){
Varchangeid. Push (updatedate [I]. Data. ID );
Pagedto = pagedto. Concat (ext. encode (updatedate [I]. getchanges (); // getchanges obtains the modified fields and values.
If (I + 1 )! = Updatedate. Length ){
Pagedto = pagedto. Concat (",");
}
}
Pagedto = pagedto. Concat ("]"); // The pagedto here is the JSON string mentioned above.
VaR xxx = new array ();
VaR vartotalsql = "";
Xxx = ext. util. JSON. Decode (pagedto );
For (VAR I = 0; I <XXX. length; I ++) {// The first loop loops three objects.
For (M in XXX [I]) {// The second loop loops the attributes and values of each object.
Vartotalsql + = m + "= '" + XXX [I] [m] + "',"; // splice attributes and values into fields to be updated in SQL
}
Vartotalsql = vartotalsql. substring (0, vartotalsql. Length-1); // remove the last one,
Vartotalsql + = "where id = '" + varchangeid [I] + "' $"; // you can obtain three SQL statements separated by "$.
}

Split $ in the background to get an array and update the database cyclically.

Updatetable. aspx. CS

Protected void page_load (Object sender, eventargs E)
{
Response. Clear ();
Sqldal. shellurl Wo = new sqldal. shellurl ();
String strjson = request. Params ["testp"]. tostring ();
String [] arrjson = strjson. Split (New char [] {'$'}, stringsplitoptions. removeemptyentries );
Foreach (string I in arrjson)
{
Wo. bolupallsql ("Update Shell set" + I );
}

}

The. NET deserialization class is not clear, and the third-party. dll is not integrated. I had to use Js for parsing.

Of course, it is still risky to split three SQL statements by $. What if there is $ in the SQL statement? The solution is to call Ajax in the JS loop to execute updatetable. aspx. CS,

Of course, updatetable. aspx. CS needs to be changed to non-circular.

The foundation is not strong, and the ground is rolling. Looking at the development of the technology for a day, we cannot keep up with it. In addition to sighs, we only have to comfort ourselves: Let's look back at "let's get down to JavaScript" and consolidate the foundation, everything is under control.

 

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.