Jqeury uses eval to convert strings to JSON

Source: Internet
Author: User

in var data = eval ("(" + MSG.D +) ") you have to pay attention, it is easy to come out is missing}, cause is in the returned string has" "This character," "has the function of escaping all this error will occur

$.ajax ({
Type: "Post",
ContentType: "Application/json",
URL: ".. /webform1.asp Tutorial X/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 processing code

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

The Eval function is evaluated as a string of numeric expressions, and its syntax is:

eval (expr)

Here expr is a string parameter that is evaluated. If the string is an expression, Eval asks for the value of the expression, and if the parameter represents one or more Web page effects statements, then Eval executes the statements. The Eval function can be used to convert a date from one format (always a string) to a numeric expression or a number

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.