My Java web application submits an AJAX request that returns JSON such:
{'value': 'aériennes'}
When 'aériennes' is displayed in the webpage, it appears as 'a�riennes', so I guess there's some kind of character encoding problem. The AJAX response headers include
Content-Type application/json
which doesn't appear to include any charset information. I guess this needs to be changed to something like
Content-Type text/html; charset=iso-8859-1 (or charset=utf8)
The server-side of the app is Spring MVC, and I guess there must be a way to set the default charset for each response?
The default encoding is UTF-8, and JSON texts that are encoded in UTF-8 are interoperable in the sense that they will be read successfully by the maximum number of implementations; there are many implementations that cannot successfully read texts in other encodings (such as UTF-16 and UTF-32).
Textual JSON data always uses the Unicode character set.
The JSON specification states that JSON strings can contain unicode characters in the form of: "here comes a unicode character: \u05d9 !" My JSON parser tries to map JSON strings to std::string so usually, one character of the JSON strings becomes one character of the std::string .
The symptoms indicate that the JSON string which was originally in UTF-8 encoding was written to the HTTP response using ISO-8859-1 encoding and the webbrowser was instructed to display it as UTF-8. If it was written using UTF-8 and displayed as ISO-8859-1, then you would have seen aériennes
. If it was written and displayed using ISO-8859-1, then you would have seen a�riennes
.
To fix the problem of the JSON string incorrectly been written as ISO-8859-1, you need to configure your webapp / Spring to use UTF-8 as HTTP response encoding. Basically, it should be doing the following under the covers:
response.setCharacterEncoding("UTF-8");
Don't change your content type header. It's perfectly fine for JSON and it is been displayed as UTF-8.
I don´t know if this is relevant anymore, but I fixed it with the @RequestMapping annotation.
@RequestMapping(method=RequestMethod.GET, produces={"application/json; charset=UTF-8"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With