3

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!

3
  • Example code? Python encoding string? Commented Dec 4, 2013 at 14:56
  • What does "didn't work well in IE8" mean? The first result is acceptable if that's the correct value. Commented Dec 4, 2013 at 14:58
  • in IE8, through I decode my string, but it will still display as unicode format, decode didn't work Commented Dec 4, 2013 at 15:04

1 Answer 1

5

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," ?

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

3 Comments

actually, till now, my response works well in all explorers, but IE8.
thanks for your help, PS, why ensure_ascii's default value is True, I think it should be False.
@matthewxiang, I don't know the reason why True is the default.

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.