问题描述
我通过字典返回我的views.py。
{injured_json:[{pk 24,model:appvisual.injured_count,fields:{Y_2010:75445,Y_2008:70251,Y_2009:70504,Y_2004:57283,Y_2005:62006,Y_2006 :64,Y_2007:71099,State_UT:Tamil Nadu,Y_2003:55242,Y_2011:74245}}],total_json:[{pk:23,model Y_2006:64996,Y_2008:60409,Y_2009:60794,Y_2004:52508,Y_2005:53866,Y_2006:55145,Y_2007 59140State_UT:Tamil Nadu,Y_2003:51025,Y_2011:65873}}],killed_json:[{pk:24,model:appvisual.killed_count :Y_2008:75445,Y_2008:70251,Y_2009:70504,Y_2004:57283,Y_2005:62006,Y_2006:64342,Y_2007:71099,State_UT Y_2003:55242Y_2011:74245}},{pk:60,model:appvisual.killed_count,fields:{Y_2010:15409,Y_2008 12784,Y_2009:13746,Y_2004:9507,Y_2005:9758,Y_2 006:11009Y_2007:12036State_UT:Tamil Nadu,Y_2003:9275,Y_2011:15422}}}}
pre>
({injured_json:[{pk:24,model:appvisual.injured_count),fields:{Y_2010:75445,Y_2008:70251,Y_2009:70504,Y_2004:57283,Y_2005:62006,Y_2006 :64342,Y_2007:71099,State_UT:Tamil Nadu,Y_2003:55242,Y_2011:74245}},total_json:[{pk:23,model:appvisual.total_accident,fields:{Y_2010:64996,Y_2008: 60949,Y_2009:60794,Y_2004:52508,Y_2005:53866,Y_2006:55145,Y_2007:59140,State_UT:Tamil Nadu,Y_2003:51025,Y_2011:65873}}],dead_json:[{pk: appvisual.killed_count,字段:{Y_2010:75445,Y_2008:70251,Y_2009:70504,Y_2004:57283,Y_2005:62006,Y_2006:64342,Y_2007:71099,State_UT:Tamil Nadu,Y_2003:55242,Y_2011: 74245}},{pk:60,model:appvisual.killed_count,fields:{Y_2010:15409,Y_2008:12784,Y_2009:13746,Y_2004:95 07,Y_2005:9758,Y_2006:11009,Y_2007:12036,State_UT:Tamil Nadu,Y_2003:9275,Y_2011:15422}}}}
由于另外添加了(和)我无法在javascript中解析json dta。我如何消除这种语法错误。
我的Views.py
def get_details(request):
import pdb; pdb.set_trace();
total_details = total_accident.objects.filter(State_UT ='Tamil Nadu')
total_details = serializers.serialize('python',total_details)
killed_details = Killed_Count.objects.filter(State_UT =
killed_details = serializers.serialize('python',killed_details)
injured_details = Injured_Count.objects.filter(State_UT ='Tamil Nadu')
injured_details = serializers.serialize(' python',injured_details)
page_data = {
total_json:total_details,
killed_json:killed_details,
injured_json:injured_details,
}
page_data = simplejson.dumps(page_data)
打印page_data
返回render_to_response('dvslzer.html',{'page_data':page_data})
我的脚本:
function test(){
var dataRows = {{page_data}};
console.log(dataRows.toSource());
var data = JSON.parse(dataRows.total_accident); // throws syntax error
console.log(data [0] .pk);
};
有什么解决方案可以摆脱这种语法错误吗?
将我们在上述评论中设定的内容拼凑在一起,这一行:
code> var data = JSON.parse(dataRows.total_accident);
...应该是:
var data = dataRows.total_json;
因为首先在对象中没有属性名为 total_accident
,其次尝试使用 JSON.parse()
是没有意义的,因为在这一点上你实际上并不处理JSON。 >
(JS根本没有真正处理JSON,因为服务器端 {{page_data}}
输出JSON直接进入页面源,所以在浏览器看到它之前,它只是在你的JS代码中显示为一个对象文字,如果是JSON,你需要使用 JSON.parse()
$ dataRows
,然后您可以开始使用点号表示访问属性。)
I am returning this in my views.py through a dictionary
{"injured_json": [{"pk": 24, "model": "appvisual.injured_count", "fields": {"Y_2010": 75445, "Y_2008": 70251, "Y_2009": 70504, "Y_2004": 57283, "Y_2005": 62006, "Y_2006": 64342, "Y_2007": 71099, "State_UT": "Tamil Nadu", "Y_2003": 55242, "Y_2011": 74245}}], "total_json": [{"pk": 23, "model": "appvisual.total_accident", "fields": {"Y_2010": 64996, "Y_2008": 60409, "Y_2009": 60794, "Y_2004": 52508, "Y_2005": 53866, "Y_2006": 55145, "Y_2007": 59140, "State_UT": "Tamil Nadu", "Y_2003": 51025, "Y_2011": 65873}}], "killed_json": [{"pk": 24, "model": "appvisual.killed_count", "fields": {"Y_2010": 75445, "Y_2008": 70251, "Y_2009": 70504, "Y_2004": 57283, "Y_2005": 62006, "Y_2006": 64342, "Y_2007": 71099, "State_UT": "Tamil Nadu", "Y_2003": 55242, "Y_2011": 74245}}, {"pk": 60, "model": "appvisual.killed_count", "fields": {"Y_2010": 15409, "Y_2008": 12784, "Y_2009": 13746, "Y_2004": 9507, "Y_2005": 9758, "Y_2006": 11009, "Y_2007": 12036, "State_UT": "Tamil Nadu", "Y_2003": 9275, "Y_2011": 15422}}]}
while retrieving the about json in javascript, the json data gets enclosed with ( and ) as follows :
({injured_json:[{pk:24, model:"appvisual.injured_count", fields:{Y_2010:75445, Y_2008:70251, Y_2009:70504, Y_2004:57283, Y_2005:62006, Y_2006:64342, Y_2007:71099, State_UT:"Tamil Nadu", Y_2003:55242, Y_2011:74245}}], total_json:[{pk:23, model:"appvisual.total_accident", fields:{Y_2010:64996, Y_2008:60409, Y_2009:60794, Y_2004:52508, Y_2005:53866, Y_2006:55145, Y_2007:59140, State_UT:"Tamil Nadu", Y_2003:51025, Y_2011:65873}}], killed_json:[{pk:24, model:"appvisual.killed_count", fields:{Y_2010:75445, Y_2008:70251, Y_2009:70504, Y_2004:57283, Y_2005:62006, Y_2006:64342, Y_2007:71099, State_UT:"Tamil Nadu", Y_2003:55242, Y_2011:74245}}, {pk:60, model:"appvisual.killed_count", fields:{Y_2010:15409, Y_2008:12784, Y_2009:13746, Y_2004:9507, Y_2005:9758, Y_2006:11009, Y_2007:12036, State_UT:"Tamil Nadu", Y_2003:9275, Y_2011:15422}}]})
Because of additionally added "(" and ")" i could not parse the json dta in javascript. How can i eliminate this syntax error.
My Views.py
def get_details(request):
import pdb;pdb.set_trace();
total_details = total_accident.objects.filter(State_UT='Tamil Nadu')
total_details = serializers.serialize('python', total_details)
killed_details = Killed_Count.objects.filter(State_UT='Tamil Nadu')
killed_details = serializers.serialize('python', killed_details)
injured_details = Injured_Count.objects.filter(State_UT='Tamil Nadu')
injured_details = serializers.serialize('python', injured_details)
page_data = {
"total_json" : total_details,
"killed_json" : killed_details,
"injured_json" : injured_details,
}
page_data= simplejson.dumps(page_data)
print page_data
return render_to_response('dvslzer.html', {'page_data':page_data})
My Script:
function test() {
var dataRows = {{page_data}};
console.log(dataRows.toSource());
var data=JSON.parse(dataRows.total_accident); // throws syntax error
console.log(data[0].pk);
};
Is there any solution to get rid of this syntax error??
Piecing together what we managed to establish in comments above, this line:
var data=JSON.parse(dataRows.total_accident);
...should actually be:
var data = dataRows.total_json;
Because firstly there is no property in the object called total_accident
, and secondly it doesn't make sense to try to use JSON.parse()
because you're not actually dealing with JSON at that point.
(The JS isn't really dealing with JSON at all, because the server-side {{page_data}}
outputs the JSON directly into the page source, so by the time the browser sees it it just appears as an object literal in your JS code. If it was JSON you'd need to use JSON.parse()
on dataRows
before you could start accessing properties with dot notation.)
这篇关于在JavaScript中解析Json数据时出现语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!