简体   繁体   中英

Django, Javascript, JSON and Unicode

I have a frustrating problem. I have a Django web app. The model contains various CharField columns. When I convert these strings into JSON using json.dumps, the strings come out as Unicode like this:

"{'field': u'value'}"

and so forth. However, I need to pass this to Javascript, and the jQuery parser croaks on this format. What I am doing is surely a very common task, but I can't seem to find how to solve it.

Any help would be great.

Which Python Version are you using? Do you use the json module from the standard library?

At least under Python 2.6.4 I get the following results:

>>> import json
>>> e = {'field': u'value'}
>>> json.dumps(e)
'{"field": "value"}'
>>> e = {'field': u'vaäüßlue'}
>>> json.dumps(e)
'{"field": "va\\u00e4\\u00fc\\u00dflue"}'
>>> 

So either you're not really converting them into JSON or your code is wrong and does not use the converted value or if you don't use the module from the standard library, the one that you are actually using has some problems with unicode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM