Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Javascript encoding

    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 " :
    Code:
    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

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    this

    simplejson.loads

    should do the opposite of

    simplejson.dumps

  • Users who have thanked chris0 for this post:

    grneyeddvl (10-06-2011)

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by chris0 View Post
    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.

  • #4
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    JSON doesn't have to be decoded by Javascript —a JSON string is a Javascript object literal, so it just needs to be evaluated.

  • Users who have thanked venegal for this post:

    grneyeddvl (10-04-2011)

  • #5
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by venegal View Post
    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 :

    Code:
    {"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.

  • #6
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.

  • Users who have thanked venegal for this post:

    grneyeddvl (10-04-2011)

  • #7
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by venegal View Post
    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.

  • #8
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi venegal
    I think i have managed to sort this out:

    Python
    Code:
    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
    Code:
    var archives = '{{archive_list|safe }}'
    getArchives(archives);
    Output:
    Code:
    {"archives" : [{"id": 1 }]}
    I presume this is what is needed to now traverse the JSON object data structure.
    Thanks again.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •