Home > Web Front-end > JS Tutorial > jqeury eval method to convert string to json_jquery

jqeury eval method to convert string to json_jquery

WBOY
Release: 2016-05-16 18:11:26
Original
1135 people have browsed it

Front page

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

///
/// Convert DataTable to Json
///

/// josn name
/// Dataset to be converted
/ //
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 method to convert DataTable into a string. The problem I encountered today was var data = eval("(" msg.d ")");
An error occurred. The prompt was missing }. This method has been used many times. I don’t know why such an error occurred.
After searching for a long time, I found out that it was a problem with the data. There is the character "" in the returned string, and "" has the function of escaping.
This error will appear in all cases. I feel dizzy. It really hurts me. I hope you won't be as confused as me.
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