Reputation: 217
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.
Upvotes: 0
Views: 2095
Reputation: 46756
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.
Upvotes: 4