我通过这个函数从datatable生成json:
public string DataTableToJSONWithStringBuilder3(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)
{
if (j == 1)
{
JSONString.Append("\"[" + table.Rows[i][j].ToString() + "]\",");
}
else
{
JSONString.Append("\"[" + table.Rows[i][j].ToString() + "]\",");
}
}
else if (j == table.Columns.Count - 1)
{
if (j == 1)
{
JSONString.Append("\"[" + table.Rows[i][j].ToString() + "]\"");
}
else
{
JSONString.Append("\"[" + table.Rows[i][j].ToString() + "]\"");
}
}
}
if (i == table.Rows.Count - 1)
{
JSONString.Append("");
}
else
{
JSONString.Append(",");
}
}
JSONString.Append("]");
}
return JSONString.ToString();
}
然后我通过ajax调用方法来检索返回值:
$.ajax({
type: "POST",
url: "/ProjMonitor/Report/ProjectMonitoringSummary.aspx/GetlineChart",
contentType: "application/json",
dataType: "json",
data: "{id:'"+idproyek+"'}",
// contentType: "application/json; charset=utf-8",
success: function (msg) {
var data = msg.d
}
});
msg或从服务器收到的响应:
{"d":"{ \"dataTarget\":[\"[Date.UTC(2016,3,01),10.00]\",\"[Date.UTC(2016,1,01),5.00]\"], \"dataRealisasi\" :[\"[Date.UTC(2016,3,01),10.00]\",\"[Date.UTC(2016,1,01),5.00]\"]}"}
我需要一个没有双引号的变量值:
[{
"name": "Proyeksi Target",
"data" : [
[ Date.UTC(2016, 3, 01), 10.00 ],
[ Date.UTC(2016, 1, 01), 5.00 ]
]
}, {
"name": "Realisasi",
"data": [
[Date.UTC(2016, 3, 01), 10.00 ],
[Date.UTC(2016, 1, 01), 5.00 ]
]
}]
你们能帮助我吗?
我试过JSON.Parse(msg.d)但错误:未捕获TypeError:JSON.Parse不是函数
我真正需要的是将highcharts JS元素的动态值放在下面,dataTarget和dataRealisasi可以通过使用参数thru jquery ajax调用方法来更改。
$('#container3').highcharts({
chart: {
type: 'spline'
},
title: {
text: 'Monitoring Proyek'
},
subtitle: {
text: 'Proyek'
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // don't display the dummy year
month: '%e. %b',
year: '%b'
},
title: {
text: 'Date'
}
},
yAxis: {
title: {
text: 'Target (%)'
},
min: 0
},
tooltip: {
headerFormat: '<b>{series.name}</b><br>',
pointFormat: '{point.x:%e. %b}: {point.y:.2f} %'
},
plotOptions: {
spline: {
marker: {
enabled: true
}
}
},
series: [{
"name": "Proyeksi Target",
"data": [ dataTarget
]
}, {
name: 'Realisasi',
data: [
dataRealisasi
]
}]
});
dataTarget和dataRealisasi的格式应该是这样的
[
[Date.UTC(1970, 9, 29), 0],
[Date.UTC(1970, 10, 9), 0.4],
[Date.UTC(1970, 11, 1), 0.25],
[Date.UTC(1971, 0, 1), 1.66],
[Date.UTC(1971, 0, 10), 1.8],
[Date.UTC(1971, 1, 19), 1.76],
[Date.UTC(1971, 2, 25), 2.62],
[Date.UTC(1971, 3, 19), 2.41],
[Date.UTC(1971, 3, 30), 2.05],
[Date.UTC(1971, 4, 14), 1.7],
[Date.UTC(1971, 4, 24), 1.1],
[Date.UTC(1971, 5, 10), 0]
]
没有报价..请帮忙
答案 0 :(得分:2)
您可以使用JSON.parse()
来解析JSON字符串。
var array = JSON.parse(object.d);
您没有有效的JSON,因为
另一种解决方案,我不建议,在这种情况下是
array = eval(object.d);
更好的方法是以不使用eval()
的方式组织数据。
答案 1 :(得分:0)
使用此字符串的唯一方法是使用ReGex删除JSON对象或数组的范围之外的内容。
var str = '{' +
'"d": "[{' +
'"name": "Proyeksi Target",' +
'"data" : [' +
'[ Date.UTC(2016, 3, 01), 10.00 ],' +
'[ Date.UTC(2016, 1, 01), 5.00 ]' +
']' +
'}, {' +
'"name": "Realisasi",' +
'"data": [' +
'[Date.UTC(2016, 3, 01), 10.00 ],' +
'[Date.UTC(2016, 1, 01), 5.00]' +
']' +
'}]"' +
'}';
// fix syntax errors
str = str.match(/("\[\{".*}]")/g)[0];
str = str.substr(1, str.length - 2).replace(/(Date.UTC[^)]+\))/g, '"$1"');
// convert to object
var jsonObj = JSON.parse(str);
var myStr = JSON.stringify(jsonObj, null, 2);
document.write(myStr);