Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12655

Re: convert python List to javascript array

References <1314977665.30984.YahooMailNeo@web160510.mail.bf1.yahoo.com>
Date 2011-09-02 10:26 -0700
Subject Re: convert python List to javascript array
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.713.1314984377.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Sep 2, 2011 at 8:34 AM, Vineet Deodhar <d_vineet@yahoo.com> 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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: convert python List to javascript array Chris Rebert <clp2@rebertia.com> - 2011-09-02 10:26 -0700

csiph-web