Convert Javascript array to python list?

I want to make goas trasnlate script. I make a request to translate.google.com and google returns an array, but the array contains undefined items.You can imagine that the answer comes as a string. I can remove commas if there is more than once in a row with regex, etc., but I'm looking for a better solution :)

How can I convert this javascript array to a python list?

["a","b",,,"e"]

My script: http://ideone.com/jhjZe

+3
source share
1 answer

JavaScript Part - Coding

In Javascript, you:

var arr = ["a","b",,,"e"];
var json_string = JSON.stringify(arr);

then you somehow pass json_string(now equal to the string " ["a","b",null,null,"e"]") from JavaScript to Python.

Python Part - Decoding

Then on the Python side, do:

json_string = '["a","b",null,null,"e"]'  # passed from JavaScript

try:
    import simplejson as json
except (ImportError,):
    import json

result = json.loads(json_string)

[u'a', u'b', None, None, u'e'] Python.

. :

:

  • JSON.stringify() JavaScript, , Chrome, Firefox, Opera, Safari IE 8.0 ( ),
  • json Python ( simplejson , , ), ,

, .

+12

All Articles