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


Groups > comp.lang.python > #12655 > unrolled thread

Re: convert python List to javascript array

Started byChris Rebert <clp2@rebertia.com>
First post2011-09-02 10:26 -0700
Last post2011-09-02 10:26 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#12655 — Re: convert python List to javascript array

FromChris Rebert <clp2@rebertia.com>
Date2011-09-02 10:26 -0700
SubjectRe: convert python List to javascript array
Message-ID<mailman.713.1314984377.27778.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web