...

View Full Version : Javascript encoding



grneyeddvl
10-02-2011, 09:24 PM
Hi

I am using Django to build my website i have some data on the server side which gets passed into my templates, this data become encoded using simplejson which is django python import

my data structure looks something like this (but with a few more dictionary's
thrown in " :



archive = ({'id' :year.id, 'months' : allMonths})
encodedData=simplejson.dumps(archive)


The data is successful encoded and i pass the list into an external java script file, Now i need to figure out how to decode the data inside a JS function can anybody shed some light on this or even point my in the right direction.

Thanks for reading

chris0
10-03-2011, 12:38 AM
this

simplejson.loads

should do the opposite of

simplejson.dumps

grneyeddvl
10-03-2011, 12:09 PM
this

simplejson.loads

should do the opposite of

simplejson.dumps

Hi thanks for the reply,
using simplejson.loads() requires the data structure to be called inside Python, how would one decode this inside of javascript function on the client side?
any ideas?

Thanks.

venegal
10-03-2011, 02:05 PM
JSON doesn't have to be decoded by Javascript —a JSON string is a Javascript object literal, so it just needs to be evaluated.

grneyeddvl
10-04-2011, 02:30 PM
JSON doesn't have to be decoded by Javascript a JSON string is a Javascript object literal, so it just needs to be evaluated.

Hi
still no luck over the last day or so.
I passed the JSON object into the external javascript file and wrapped it into the alert function the browser came back with this :



{"archives" : [{"id&quot: 1&quot}]};


is there a sound way of traversing over the object i'm not getting any response with eval, maybe because its not a string?.

Thanks again.

venegal
10-04-2011, 02:49 PM
It seems you are not dumping the JSON object as plaintext, but with encoded HTML entities (if those "s in there were "s, you would be fine).

If this were PHP, I'd tell you you got a call to htmlentities() in there you need to get rid of, but I don't know anything about Python.

You say you write that JSON string and the alert (or evaluation) out into an external Javascript file why don't you just post the contents of that file, then I can tell you what the problem with it is, and you can go from there to figure out what's wrong with your server side code.

grneyeddvl
10-04-2011, 03:33 PM
It seems you are not dumping the JSON object as plaintext, but with encoded HTML entities (if those "s in there were "s, you would be fine).

If this were PHP, I'd tell you you got a call to htmlentities() in there you need to get rid of, but I don't know anything about Python.

You say you write that JSON string and the alert (or evaluation) out into an external Javascript file why don't you just post the contents of that file, then I can tell you what the problem with it is, and you can go from there to figure out what's wrong with your server side code.

Thank you once for trying to help me with this, what i will do is spend abit more time with python and the encoding styles and see if i cannot come up with some else regarding JSON. If not i will return thanks again now i know that it's wrapped in html and not as plain text.

Thanks.

grneyeddvl
10-04-2011, 07:58 PM
Hi venegal
I think i have managed to sort this out:

Python


mydata={'archives': archive }
return json.dumps(mydata)


Added a new pipe safe tag not sure what this does but it seems to have worked

Javascript


var archives = '{{archive_list|safe }}'
getArchives(archives);


Output:


{"archives" : [{"id": 1 }]}


I presume this is what is needed to now traverse the JSON object data structure.
Thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum