I need to convert a Python dictionary into a JavaScript dictionary. From what I understand, I have to first convert the Python dict into JSON format and then transform it into a JavaScript Object.
view.py
jsonheaderdict = json.dumps(headerdict)
{{jsonheaderdict}}
in the template produces:
{"F 1": ["BBBB", "AAAAA"], "F 2": ["ASDASD"], "F 3": ["QWEQWE"]}
My JavaScript code is as follows:
$(".dict").click(function () {
alert("first alert");
var result = JSON.parse(jsonheaderdict);
alert(result);
});
The first alert is displayed, but not the second one. What am I missing? I've also tried using
var result = jQuery.parseJSON(jsonheaderdict);
, which didn't work either. I've searched for similar questions but haven't found a solution that works for me.
EDIT
To better understand how jsonheaderdict
is created in my view:
headerdict = dict()
for d in projectdescriptors:
dp = projectprojections.filter(descriptor=d)
parray = []
for p in dp:
parray.append(p.name)
headerdict[d.name] = parray