JSON character encoding

201,876

Solution 1

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.

Solution 2

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"})

Solution 3

First, your posted data isn't valid JSON. This would be:

{"value": "aériennes"}

Note the double quotes: They are required.

The Content-Type for JSON data should be application/json. The actual JSON data (what we have above) should be encoded using UTF-8, UTF-16, or UTF-32 - I'd recommend using UTF-8.

You can use a tool like Wireshark to monitor network traffic and see how the data looks, you should see the bytes c3 89 for the é. I've never worked with Spring, but if it's doing the JSON encoding, this is probably taken care of properly, for you.

Once the JSON reaches the browser, it should good, if it is valid. However, how are you inserting the data from the JSON response into the webpage?

Solution 4

finally I got the solution:

Only put this line

@RequestMapping(value = "/YOUR_URL_Name",method = RequestMethod.POST,produces = "application/json; charset=utf-8")

this will definitely help.

Solution 5

That happened to me exactly the same with this:

<%@ page language="java" contentType="application/json" pageEncoding="UTF-8"%>

But this works for me:

<%@ page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>

Try adding

 ;charset=UTF-8

to your contentType.

Share:
201,876
Dónal
Author by

Dónal

I earn a living by editing text files. I can be contacted at: [email protected] You can find out about all the different kinds of text files I've edited at: My StackOverflow Careers profile

Updated on July 08, 2022

Comments

  • Dónal
    Dónal almost 2 years

    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?