JSON character encoding

Viewed 210992

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?

11 Answers

You should set response encoding before you write your answer in the response.

It can be annotation, response.setContentEncoding(String s), or even response.setHeader(String s1, String s2).

Once you start to use response output or writer, defined or guessed encoding will be used to encode characters in the result binary data. That is why you will not have an option to change it afterwards.

Related