python 传递数组结构以查看 json
python passing array structure to view as json
Python API returns the following data when requested::
x_values = [
[0,'10:00:00 AM'],
[1,'10:00:10 AM'],
[2,'10:00:10 AM'],
[3,'10:00:20 AM']
]
y_values = [
[0,3],
[1,0],
[2,3],
[3,1]
]
data = {
"x":x_values,
"y":y_values
}
self.send_json(data)
这是 api 使用 Python 2.7.9。以下是 jquery 脚本。
$('.flot-graph').each(function() {
var graph_drawing_area = $(this);
$.ajax({
url: '/api/get/data', // call the api
success: function(data){
var y = JSON.stringify(data['y']);
var x = JSON.stringify(data['x']);
var graph_data = [
{
data: y
}
];
var options = {
xaxis: {
ticks:x
}
};
return $.plot(graph_drawing_area, graph_data, options);
},
error: function(){
alert('failure');
}
});
});
这应该在名称为 class 'flot-graph' 的 div 上绘制图表。我现在遇到的问题是我需要重新生成与 python 中完全相同的数组结构。换句话说,jquery 脚本必须类似于
success: function(data){
...
var graph_data = [{
//note that y is decoded and replaced as array format
data: [
[0,3],
[1,0],
[2,3],
[3,1]
]
}];
var options = {
//note that x is decoded and replaced as array format
xaxis: {
ticks:[
[0,'10:00:00 AM'],
[1,'10:00:10 AM'],
[2,'10:00:10 AM'],
[3,'10:00:20 AM']
]
}
};
return $.plot(graph_drawing_area, graph_data, options);
},
.......
我尝试使用 JSON.stringify,但似乎没有重新生成数组。 JSON.parse() 也没有给我我想要的东西,但是一堆 "Uncaught SyntaxError: Unexpected token u" 错误 - 我假设 return 解析后的值在这里不是一个合适的选项。
我该如何解决这个问题?如果我的方法不是最好的,在这种情况下哪种方法更合适?
谢谢。
我根本不需要解析它...多么愚蠢:'(
Python API returns the following data when requested::
x_values = [
[0,'10:00:00 AM'],
[1,'10:00:10 AM'],
[2,'10:00:10 AM'],
[3,'10:00:20 AM']
]
y_values = [
[0,3],
[1,0],
[2,3],
[3,1]
]
data = {
"x":x_values,
"y":y_values
}
self.send_json(data)
这是 api 使用 Python 2.7.9。以下是 jquery 脚本。
$('.flot-graph').each(function() {
var graph_drawing_area = $(this);
$.ajax({
url: '/api/get/data', // call the api
success: function(data){
var y = JSON.stringify(data['y']);
var x = JSON.stringify(data['x']);
var graph_data = [
{
data: y
}
];
var options = {
xaxis: {
ticks:x
}
};
return $.plot(graph_drawing_area, graph_data, options);
},
error: function(){
alert('failure');
}
});
});
这应该在名称为 class 'flot-graph' 的 div 上绘制图表。我现在遇到的问题是我需要重新生成与 python 中完全相同的数组结构。换句话说,jquery 脚本必须类似于
success: function(data){
...
var graph_data = [{
//note that y is decoded and replaced as array format
data: [
[0,3],
[1,0],
[2,3],
[3,1]
]
}];
var options = {
//note that x is decoded and replaced as array format
xaxis: {
ticks:[
[0,'10:00:00 AM'],
[1,'10:00:10 AM'],
[2,'10:00:10 AM'],
[3,'10:00:20 AM']
]
}
};
return $.plot(graph_drawing_area, graph_data, options);
},
.......
我尝试使用 JSON.stringify,但似乎没有重新生成数组。 JSON.parse() 也没有给我我想要的东西,但是一堆 "Uncaught SyntaxError: Unexpected token u" 错误 - 我假设 return 解析后的值在这里不是一个合适的选项。
我该如何解决这个问题?如果我的方法不是最好的,在这种情况下哪种方法更合适?
谢谢。
我根本不需要解析它...多么愚蠢:'(