开发者

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.dump开发者_高级运维s, 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜