Jqeury eval How to convert a string to JSON _jquery

Source: Internet
Author: User
Tags eval string to json
Front Page

Copy Code code as follows:

$.ajax ({
Type: "Post",
ContentType: "Application/json",
URL: ".. /webform1.aspx/getrightsstr ",
DataType: "JSON",
Success:function (msg) {
alert (MSG.D);
var data = eval ("+ MSG.D +"));
$.each (data.rights, function (index, item) {
Alert (item. Rightsname);
});
},
Error:function (E, S, D) {
Alert (e);
alert (s);
alert (d);
}
});

Background method:
Copy Code code as follows:

<summary>
The DataTable turns into JSON
</summary>
<param name= "Jsonname" >josn name </param>
<param name= "DT" > Data set to be converted </param>
<returns></returns>
public static string Datatabletojson (String jsonname, DataTable DT)
{
StringBuilder Json = new StringBuilder ();
Json.append ("{\" "+ Jsonname +" \ ": [");
if (dt. Rows.Count > 0)
{
for (int i = 0; i < dt. Rows.Count; i++)
{
Json.append ("{");
for (int j = 0; j < dt.) Columns.count; J + +)
{
Json.append ("\" "+ dt.) COLUMNS[J]. Columnname.tostring () + "\": \ "" + dt. ROWS[I][J]. ToString () + "\" ");
if (J < dt. COLUMNS.COUNT-1)
{
Json.append (",");
}
}
Json.append ("}");
if (i < dt. ROWS.COUNT-1)
{
Json.append (",");
}
}
}
Json.append ("]}");
return json.tostring ();
}

This method is a way to convert a DataTable into a string. The problem encountered today is in var data = eval ("+ MSG.D +"));
Error prompted is missing} This method has been useful for many times without knowing why such a mistake occurred.
Looking for a long time, only to find that the problem is the data in the returned string has "\" the character, "\" has the function of escaping
All will appear this error, Halo Ah, the real harm to me, I hope you do not like me so confused.

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.