I need to change JSON format by JavaScript or C#... The method in code behind that generate JSON data is like this:
public string DataTableToJSONWithStringBuilder(DataTable table)
{
var JSONString = new StringBuilder();
if (table.Rows.Count > 0)
{
JSONString.Append("[");
for (int i = 0; i < table.Rows.Count; i++)
{
JSONString.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
if (j < table.Columns.Count - 1)
{
JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\",");
}
else if (j == table.Columns.Count - 1)
{
JSONString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\"");
}
}
if (i == table.Rows.Count - 1)
{
JSONString.Append("}");
}
else
{
JSONString.Append("},");
}
}
JSONString.Append("]");
}
return JSONString.ToString();
}
I access the method through jQuery AJAX
var tahun = $("#<%=txbTahun.ClientID %>").val();
$.ajax({
type: "POST",
url: "/ProjMonitor/Report/ProjectMonitoringSummary.aspx/GetData",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{tahun:" + tahun + "}",
contentType: "application/json; charset=utf-8",
success: function (msg) {
var dataLevel = JSON.stringify(msg);
// alert(dataLevel);
ShowData(dataLevel,'');
},
error: function (msg) {
}
});
Then the output (dataLevel) turnsout to be like this:
{d:[
{
"1332879360000.0": 300,
"1332797760000.0": 353,
"1332799320000.0": 358,
"1332879780000.0": 302,
"1332800160000.0": 359,
"1332880200000.0": 299,
"1332880620000.0": 298,
"1332881040000.0": 301,
"1332881460000.0": 402,
"1332880020000.0": 330,
"1332882300000.0": 466,
"1332796620000.0": 519,
"1332800520000.0": 447,
"1332797460000.0": 359,
"1332801000000.0": 442
},
{
"1332879360000.0": 300,
"1332797760000.0": 353,
"1332799320000.0": 358,
"1332879780000.0": 302,
"1332800160000.0": 359,
"1332880200000.0": 299,
"1332880620000.0": 298,
"1332881040000.0": 301,
"1332881460000.0": 402,
"1332880020000.0": 330,
"1332882300000.0": 466,
"1332796620000.0": 519,
"1332800520000.0": 447,
"1332797460000.0": 359,
"1332801000000.0": 442
}
]}
I need the format to be like this... What should I change or what JavaScript function that can convert it?
[
{
"1332879360000.0": 300,
"1332797760000.0": 353,
"1332799320000.0": 358,
"1332879780000.0": 302,
"1332800160000.0": 359,
"1332880200000.0": 299,
"1332880620000.0": 298,
"1332881040000.0": 301,
"1332881460000.0": 402,
"1332880020000.0": 330,
"1332882300000.0": 466,
"1332796620000.0": 519,
"1332800520000.0": 447,
"1332797460000.0": 359,
"1332801000000.0": 442
},
{
"1332879360000.0": 300,
"1332797760000.0": 353,
"1332799320000.0": 358,
"1332879780000.0": 302,
"1332800160000.0": 359,
"1332880200000.0": 299,
"1332880620000.0": 298,
"1332881040000.0": 301,
"1332881460000.0": 402,
"1332880020000.0": 330,
"1332882300000.0": 466,
"1332796620000.0": 519,
"1332800520000.0": 447,
"1332797460000.0": 359,
"1332801000000.0": 442
}
]
You're getting an object returned, with a property d
holding the array you want. To get the array, just do dataLevel.d
.
So...
var tahun = $("#<%=txbTahun.ClientID %>").val();
$.ajax({
type: "POST",
url: "/ProjMonitor/Report/ProjectMonitoringSummary.aspx/GetData",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{tahun:" + tahun + "}",
contentType: "application/json; charset=utf-8",
success: function (msg) {
var dataLevel = JSON.stringify(msg);
// alert(dataLevel.d);
ShowData(dataLevel.d,'');
},
error: function (msg) {
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.