我正在Web服务中将一些数据创建为字符串,然后将该字符串返回以用于jqPlot。我的问题是jqPlot期望有一个对象,并且无法将我的字符串转换为正确的格式。
这个工作示例很好,因为line1变量被构造为一个对象
var line1 = [['2007-02-18 00:00:00',4980],['2007-02-24 09:50:00',4230],['2007-05-20 00:00:00',5060]]
var plot1 = $.jqplot('chart1', [line1], {
title: 'Test Plot',
title: 'Data with Dates',
axes: { xaxis: { renderer: $.jqplot.DateAxisRenderer} },
});
在我的场景中,Web服务将第一行作为字符串返回:
line1 = "[['2007-02-18 00:00:00',4980],['2007-02-24 09:50:00',4230],['2007-05-20 00:00:00',5060]]"
我如何处理此字符串,以便可以将其用作jqPlot的数组,如在工作示例中一样?
最佳答案
既然您已经在使用jQuery,为什么不将其放入the utility function made especially for dealing with this problem中呢?
var json_string = "some_json_string",
obj = $.parseJSON(json_string);