我想将一个JSON对象作为sting放入一个javascript变量中,以便创建一个图形。
qm.createGraphData = function() {
$.post("ajax_getGraphDataWebsite ", function(json) {
qm.negativesData = json;
},"json");
qm.data = [{
"xScale":"ordinal",
"comp":[],
"main":[{
"className":".main.l1",
qm.negativesData},{
"className":".main.l2",
qm.negativesData}],
"type":"line-dotted",
"yScale":"linear"}];
}
字符串值应添加到“data”部分。现在添加了对象,但我需要将字符串值添加到变量中,如下面的示例所示:
{"data":[{"x":"3283581","y":"2013-10-16"},{"x":"1512116","y":"2013-10-17"},{"x":"3967","y":"2013-10-18"},{"x":"1094","y":"2013-10-19"},{"x":"853","y":"2013-10-20"},{"x":"1205","y":"2013-10-21"},{"x":"2618700","y":"2013-10-22"},{"x":"3928291","y":"2013-10-23"},{"x":"3670318","y":"2013-10-24"},{"x":"3347369","y":"2013-10-25"},{"x":"2525573","y":"2013-10-26"},{"x":"3224612","y":"2013-10-27"},{"x":"3992964","y":"2013-10-28"},{"x":"3949904","y":"2013-10-29"},{"x":"3568618","y":"2013-10-30"},{"x":"3104696","y":"2013-10-31"},{"x":"3246932","y":"2013-11-01"},{"x":"2817758","y":"2013-11-02"},{"x":"3198856","y":"2013-11-03"},{"x":"3952957","y":"2013-11-04"},{"x":"3934173","y":"2013-11-05"},{"x":"3878718","y":"2013-11-06"},{"x":"3642822","y":"2013-11-07"},{"x":"3186096","y":"2013-11-08"}]}
这会为我生成正确的图表。有谁知道如何将json对象转换为如上所示的字符串并将其发送到qm.negativesData变量?
//更新
现在我有了qm.negativesData = JSON.stringify(json)的字符串;溶液
但我的qm.negativesdata不会被添加到qm.data变量中...我收到控制台错误SyntaxError:invalid property id
我想我不是以正确的方式添加它们?
答案 0 :(得分:1)
要将JSON对象转换为JSON字符串,您可以尝试myObject.stringify()
,JSON.stringify(myObject)
,或者如果您使用该库的内置函数使用库。
所以,你可以这样做:qm.negativesData = myObject.stringify()
干杯