-1

I am trying to pass value from Python ta Javascript (simple JSON) but when I try I cannot create, probably because u' for UTF in python

{'username': u'Tester1', 'age': 0L}

How to convert this string to javascript dictionary ? I have tried JSON.parse, eval("(" + json + ")") but it didn't work

1 Answer 1

3

You are passing a Python object instead of JSON.

On the Python side, convert this to JSON first:

import json

json_value = json.dumps(python_object)

Demo:

>>> import json
>>> python_object = {'username': u'Tester1', 'age': 0L}
>>> print json.dumps(python_object)
{"username": "Tester1", "age": 0}

The latter you can load into JavaScript with JSON.parse().

Sign up to request clarification or add additional context in comments.

1 Comment

I happened to provide a similar solution today stackoverflow.com/a/19354394/1903116

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.