我需要通过JavaScript或C#更改JSON格式... 生成JSON数据的代码后面的方法是这样的:
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();
}
我通过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) {
}
});
然后输出(dataLevel)就像这样:
{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
}
]}
我需要这样的格式...我应该更改什么或可以转换它的JavaScript函数?
[
{
"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
}
]
答案 0 :(得分:0)
您将获得一个返回的对象,其中一个属性d
包含您想要的数组。要获取数组,只需执行dataLevel.d
。
因此...
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) {
}
});