如何在没有双引号的情况下获取json元素的值 [英] how to get the value of json element without the double quote
问题描述
我通过此函数从数据表生成json:
I generate json from datatable by this function:
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方法来获取返回值:
and then i retrieve the return value by calling the method thru 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
}
});
从服务器收到的味精或响应:
the msg or the response received from the server:
{"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]\"]}"}
我需要一个没有双引号的变量值:
I need a value of a variable like this without the double quotes:
[{
"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不是函数
i tried JSON.Parse(msg.d) but error : Uncaught TypeError: JSON.Parse is not a function
我真正需要的是将highcharts JS元素的动态值放在下面,其中dataTarget和dataRealisasiasi可以通过使用jquery ajax参数调用方法来更改.
what i really need is put the dynamic value of highcharts JS element below, with the dataTarget and dataRealisasi can be changed by calling method with parameter 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的格式应为
the format of dataTarget and dataRealisasi should be like this
[
[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]
]
没有引号..请帮助
推荐答案
You may use JSON.parse()
for parsing a JSON string.
var array = JSON.parse(object.d);
您没有有效的 JSON ,因为
- 前导零
- 括号
- 函数/方法调用
在这种情况下,我不建议使用其他解决方案
The other solution, I do not recommend, is in this case
array = eval(object.d);
更好的方法是以不使用 eval()
.
The better approach is to organize the data in the manner of not using eval()
.
这篇关于如何在没有双引号的情况下获取json元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!