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??
(), which you say get added along the way for some reason. In any case, the JSON that you show at the beginning of your question doesn't have a property calledtotal_accident, so wouldn'tdataRows.total_accidentbe undefined? Which in turn would mean thatJSON.parse(dataRows.total_accident)should give you an error...var data = dataRows.total_json;. You don't needJSON.parse()because by that stage you're not dealing with JSON, you have an actual JS object.