Create Json Using JavaScriptSerializer
I have following code in Generic Handler of Asp.net: var students= Student.GetStudents(); var result = new { Data = students.Select(s => new []{s.subjects})
Solution 1:
Its not perfect but working for me:
var rows = Row.GetRows();
StringBuilder sb = new StringBuilder("\"aaData\":[");
for (int i = 0; i < rows.Count; i++)
{
if (i == 0){ sb.Append("["); }
else { sb.Append(",["); }
for (int j = 0; j < rows[i].fields.Count; j++)
{
if (j == 0){
sb.Append("\"" + rows[i].fields[j].ToString() + "\"");
}
else{
sb.Append(",\"" + rows[i].fields[j].ToString() + "\"");
}
}
sb.Append("]");
}
sb.Append("]}");
var json = sb.ToString();
context.Response.ContentType = "application/json";
context.Response.Write(json);
Since the JavaScriptSerializer
adding one more []
to the json object, I tried not to use it and create the json string manually.
Let me know the pros and cons of using this way.
Post a Comment for "Create Json Using JavaScriptSerializer"