Java – JSON character encoding

character encodingjavajsonspring-mvc

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?

Best Answer

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.