I am roughly following this example. But must be doing something silly...
我粗略地遵循这个例子。但一定要做些傻事......
The server side Django view code:
服务器端Django查看代码:
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)
这些是从pydev调试器复制粘贴的变量的值:(我只是为每个描述字段类型使用字符串进行测试)
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):
json(由谷歌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:
接收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});
This causes the following error on constructing the DataTable object (second line):
这会导致构造DataTable对象时出现以下错误(第二行):
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.
我知道整个线索是确保模式格式与数据格式匹配,但似乎是这种情况。它必须是简单的东西。
4 个解决方案
#1
3
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?
不幸的是我不能评论所以这不完全是一个完整的答案,但是你可以尝试在尝试将它用于图表之前评估JSON吗?
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?
或者,也许玩simplejson从python转储你的json字符串而不是只返回gviz字符串?
#2
3
Make sure you put 'unsafe-eval'
for script sources in Content-Security-Policy
.
确保在Content-Security-Policy中为脚本源添加“unsafe-eval”。
A policy I use for Google Maps and Google Charts:
我用于Google地图和Google Charts的政策:
<meta
http-equiv="Content-Security-Policy"
content="
default-src 'self';
script-src 'self' 'unsafe-inline' 'unsafe-eval' maps.googleapis.com www.google.com www.google-analytics.com;
img-src 'self' csi.gstatic.com www.google-analytics.com maps.gstatic.com maps.googleapis.com;
style-src 'self' 'unsafe-inline' www.google.com fonts.googleapis.com ajax.googleapis.com;
font-src 'self' fonts.gstatic.com;
"
/>
#3
2
JSON parsers should require field names to be delimited by double quotes, as outlined in the specification JSON RFC 4627:
JSON解析器应该要求字段名称用双引号分隔,如规范JSON RFC 4627中所述:
An object is an unordered collection of zero or more name/value pairs, where a name is a string [...] A string begins and ends with quotation marks.
对象是零个或多个名称/值对的无序集合,其中名称是字符串[...]字符串以引号开头和结尾。
So, the JSON should be formatted like this:
因此,JSON应该格式如下:
{
"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"
}
]
}
]
}
#4
0
Had the same issue, even when stripping the options to an empty dictionary (resulting in the error "Invalid JSON string: {}" ...). Turns out the issue is with the script-src CSP, documented here: https://github.com/keen/keen-js/issues/394
有同样的问题,即使将选项剥离到空字典(导致错误“无效的JSON字符串:{}”...)。事实证明问题在于script-src CSP,在此处记录:https://github.com/keen/keen-js/issues/394
"Solution" is to add unsafe-eval to the CSP.
“解决方案”是将不安全评估添加到CSP。
#1
3
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?
不幸的是我不能评论所以这不完全是一个完整的答案,但是你可以尝试在尝试将它用于图表之前评估JSON吗?
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?
或者,也许玩simplejson从python转储你的json字符串而不是只返回gviz字符串?
#2
3
Make sure you put 'unsafe-eval'
for script sources in Content-Security-Policy
.
确保在Content-Security-Policy中为脚本源添加“unsafe-eval”。
A policy I use for Google Maps and Google Charts:
我用于Google地图和Google Charts的政策:
<meta
http-equiv="Content-Security-Policy"
content="
default-src 'self';
script-src 'self' 'unsafe-inline' 'unsafe-eval' maps.googleapis.com www.google.com www.google-analytics.com;
img-src 'self' csi.gstatic.com www.google-analytics.com maps.gstatic.com maps.googleapis.com;
style-src 'self' 'unsafe-inline' www.google.com fonts.googleapis.com ajax.googleapis.com;
font-src 'self' fonts.gstatic.com;
"
/>
#3
2
JSON parsers should require field names to be delimited by double quotes, as outlined in the specification JSON RFC 4627:
JSON解析器应该要求字段名称用双引号分隔,如规范JSON RFC 4627中所述:
An object is an unordered collection of zero or more name/value pairs, where a name is a string [...] A string begins and ends with quotation marks.
对象是零个或多个名称/值对的无序集合,其中名称是字符串[...]字符串以引号开头和结尾。
So, the JSON should be formatted like this:
因此,JSON应该格式如下:
{
"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"
}
]
}
]
}
#4
0
Had the same issue, even when stripping the options to an empty dictionary (resulting in the error "Invalid JSON string: {}" ...). Turns out the issue is with the script-src CSP, documented here: https://github.com/keen/keen-js/issues/394
有同样的问题,即使将选项剥离到空字典(导致错误“无效的JSON字符串:{}”...)。事实证明问题在于script-src CSP,在此处记录:https://github.com/keen/keen-js/issues/394
"Solution" is to add unsafe-eval to the CSP.
“解决方案”是将不安全评估添加到CSP。