Re: convert python List to javascript array

Discussion in 'Python' started by Chris Rebert, Sep 2, 2011.

  1. Chris Rebert

    Chris Rebert Guest

    On Fri, Sep 2, 2011 at 8:34 AM, Vineet Deodhar <> wrote:
    > Hi !
    > Within a web framework, I want want to pass a python sequence (list or
    > tuple) to client-side javascript function as an array (javascript
    > compatible)
    > e.g., I have this list:
    > L = ['spam', 'ham', 'eggs', 12, (13.63)]
    > What is the correct way to convert L to javascript array format?
    > 1) jsonify the list and pass it to javascript
    > (whether json format & javascript array are similar?)


    JSON is in fact a subset of JavaScript, and modern browsers now
    include a specific API for parsing and generating it
    (https://developer.mozilla.org/En/Using_native_JSON ).
    Python likewise has a JSON module in the std lib:
    http://docs.python.org/library/json.html

    > OR
    > 2)
    >>> import array
    >>> y = array.array(i, L)

    >  then return y to javascript function
    > But the problem with this method is, it will give an array of basic values
    > only.


    The word "array" gets tossed around a lot by programmers. The `array`
    module is not at all what you want in this case.

    Cheers,
    Chris
    --
    http://rebertia.com
     
    Chris Rebert, Sep 2, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    6,011
    John C. Bollinger
    May 20, 2005
  2. Klaus Neuner
    Replies:
    7
    Views:
    498
    Klaus Neuner
    Jul 26, 2004
  3. noydb
    Replies:
    6
    Views:
    372
    Terry Reedy
    May 14, 2011
  4. Mark Niemczyk
    Replies:
    1
    Views:
    455
    noydb
    May 13, 2011
  5. David
    Replies:
    2
    Views:
    129
    Thomas 'PointedEars' Lahn
    Sep 9, 2008
Loading...

Share This Page