Question

I got a problem, first I made a api that accepts a post request,
then responds with JSON as a result.

Post request data had been encoded, I accepted the post data, and got the data
rightly, then I response with the new data in JSON format.

But when I returned the JSON, I found that the string is unicode format, e.g.

{
  'a':'\u00e3\u0080'
}

but, I want to get a format like this:

{
  'a':"ã"
}

I want this format because I found that this unicode format didn't work well in IE8.

Yes, IE8.

What can I do for this issue? Thanks!

Was it helpful?

Solution

If you're using standard library json module, specifying ensure_ascii=False give you what you want.

For example:

>>> print json.dumps({'a': u'ã'})
{"a": "\u00e3"}
>>> print json.dumps({'a': u'ã'}, ensure_ascii=False)
{"a": "ã"}

According to json.dump documentation:

If ensure_ascii is True (the default), all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result is a str instance consisting of ASCII characters only. If ensure_ascii is False, some chunks written to fp may be unicode instances. This usually happens because the input contains unicode strings or the encoding parameter is used. ...


BTW, what do you mean "unicode format didn't work well in IE8," ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top