“无效的JSON字符串”在Google可视化API示例中 [英] "Invalid JSON string" in Google visualization API example

查看:150
本文介绍了“无效的JSON字符串”在Google可视化API示例中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大致遵循此示例 。但是一定要做一些傻事...



服务器端Django查看代码:

 code> data_table = gviz_api.DataTable(description)
data_table.LoadData(data)
json = data_table.ToJSon()
return json

这些是从pydev调试器复制粘贴的变量的值:(Im为每个描述字段类型使用字符串进行测试)



描述:

  [(sensor,string,Sensor名称),
(timestamp,string,Time),
(value,string,Sensor value)]

数据:

  [[ ','2011-05-09 16:06:43.936000','22 .0'],
['testsensor','2011-05-09 16:56:23.367000','23 .0']]

json(由google api生成):

  {cols:[{id:'sensor',label:'Sensor name',type:'string'},{id:'timestamp',label:'Time'类型:'string'},{id:'value',label:'传感器值',类型:'string'}],行:[{c:[{v:'testsensor'},{v:'2011- 05-09 16:06:43.936000'},{v:'22 .0'}]},{c:[{v:'testsensor'},{v:'2011-05-09 16:56:23.367000'}, {v:'23 .0'}]}}} 

接收json的客户端JavaScript代码:

  var json_table = new google.visualization.Table(document.getElementById('dataview')); 
var json_data = new google.visualization.DataTable(data,0.6);
json_table.draw(json_data,{showRowNumber:true});

在构造DataTable对象(第二行)时会导致以下错误:

 未捕获错误:无效的JSON字符串:{cols:[{id:'sensor',label:'传感器名称',键入:'string'}, {id:'timestamp',label:'Time',type:'string'},{id:'value',label:'Sensor value',type:'string'}],rows:[{c:[{ v:'testsensor'},{v:'2011-05-09 16:06:43.936000'},{v:'22 .0'}]},{c:[{v:'testsensor'},{v: 2011-05-09 16:56:23.367000'},{v:'23 .0'}]}}} 
在默认情况下,table.I.js:152
/ pre>

我了解到,整个线索确保模式格式与数据格式匹配,但这似乎是这样。它必须是简单的。

解决方案

不幸的是我不能评论,所以这不完全是一个完整的答案,但可以尝试使用JSON之前,先尝试使用JSON图表?

  var json_table = new google.visualization.Table(document .getElementById( '数据视图')); 
var evalledData = eval((+ data +));
var json_data = new google.visualization.DataTable(evalledData,0.6);
json_table.draw(json_data,{showRowNumber:true});

我认为这可能已经解决了我过去的这个问题;这可能不是最安全的方法,但是您至少可以尝试进行测试。



或者,也可以使用simplejson来将json字符串从python而不是只返回gviz字符串?


I am roughly following this example. But must be doing something silly...

The server side Django view code:

data_table = gviz_api.DataTable(description)
data_table.LoadData(data)
json = data_table.ToJSon()
return json

These are the values of the variables copy-pasted from the pydev debugger: (Im using strings for each description field type just for testing)

description:

[("sensor","string", "Sensor name"), 
 ("timestamp","string", "Time"),
 ("value","string", "Sensor value")]

data:

[['testsensor', '2011-05-09 16:06:43.936000', '22.0'],
['testsensor', '2011-05-09 16:56:23.367000', '23.0']]

json (generated by the google api):

{cols:[{id:'sensor',label:'Sensor name',type:'string'},{id:'timestamp',label:'Time',type:'string'},{id:'value',label:'Sensor value',type:'string'}],rows:[{c:[{v:'testsensor'},{v:'2011-05-09 16:06:43.936000'},{v:'22.0'}]},{c:[{v:'testsensor'},{v:'2011-05-09 16:56:23.367000'},{v:'23.0'}]}]}

The client side javascript code that receives the json:

var json_table = new google.visualization.Table(document.getElementById('dataview'));
var json_data = new google.visualization.DataTable(data, 0.6);
json_table.draw(json_data, {showRowNumber: true});

This causes the following error on constructing the DataTable object (second line):

Uncaught Error: Invalid JSON string: {cols:[{id:'sensor',label:'Sensor name',type:'string'},{id:'timestamp',label:'Time',type:'string'},{id:'value',label:'Sensor value',type:'string'}],rows:[{c:[{v:'testsensor'},{v:'2011-05-09 16:06:43.936000'},{v:'22.0'}]},{c:[{v:'testsensor'},{v:'2011-05-09 16:56:23.367000'},{v:'23.0'}]}]}
in default,table.I.js:152

I understood that the whole clue was making sure that the schema format matches the data format but this seems to be the case. It must be something simple.

解决方案

Unfortunately I can't comment so this isn't exactly a full answer, but could you try eval'ing the JSON before trying to use it for the chart?

var json_table = new google.visualization.Table(document.getElementById('dataview'));
var evalledData = eval("("+data+")");
var json_data = new google.visualization.DataTable(evalledData, 0.6);
json_table.draw(json_data, {showRowNumber: true});

I think that may have solved this problem for me in the past; it may not be the safest way to go about it, but you could at least try it for testing.

Alternatively, perhaps play with simplejson to dump your json string from the python instead of just returning the gviz string?

这篇关于“无效的JSON字符串”在Google可视化API示例中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆