[java] JSON character encoding

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?

This question is related to java json spring-mvc character-encoding

The answer is


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.


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?


If the suggested solutions above didn't solve your issue (as for me), this could also help:

My problem was that I was returning a json string in my response using Springs @ResponseBody. If you're doing this as well this might help.

Add the following bean to your dispatcher servlet.

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean
                class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </list>
    </property>
</bean>

(Found here: http://forum.spring.io/forum/spring-projects/web/74209-responsebody-and-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"})

Also, you can use spring annotation RequestMapping above controller class for receveing application/json;utf-8 in all responses

@Controller
@RequestMapping(produces = {"application/json; charset=UTF-8","*/*;charset=UTF-8"})
public class MyController{
 ...
}

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.


If you're using StringEntity try this, using your choice of character encoding. It handles foreign characters as well.


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.


The answers here helped me solve my problem, although it's not completely related. I use the javax.ws.rs API and the @Produces and @Consumes annotations and had this same problem - the JSON I was returning in the webservice was not in UTF-8. I solved it with the following annotations on top of my controller functions :

@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON + "; charset=UTF-8")

and

@Consumes(javax.ws.rs.core.MediaType.APPLICATION_JSON + "; charset=UTF-8")

On every endpoint's get and post function. I wasn't setting the charset and this solved it. This is part of jersey so maybe you'll have to add a maven dependency.


response.setContentType("application/json;charset=utf-8");

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?

Examples related to spring-mvc

Two Page Login with Spring Security 3.2.x ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Spring 5.0.3 RequestRejectedException: The request was rejected because the URL was not normalized The type WebMvcConfigurerAdapter is deprecated RestClientException: Could not extract response. no suitable HttpMessageConverter found Spring boot: Unable to start embedded Tomcat servlet container UnsatisfiedDependencyException: Error creating bean with name 8080 port already taken issue when trying to redeploy project from Spring Tool Suite IDE Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed Difference between the annotations @GetMapping and @RequestMapping(method = RequestMethod.GET)

Examples related to character-encoding

Changing PowerShell's default output encoding to UTF-8 JsonParseException : Illegal unquoted character ((CTRL-CHAR, code 10) Change the encoding of a file in Visual Studio Code What is the difference between utf8mb4 and utf8 charsets in MySQL? How to open html file? All inclusive Charset to avoid "java.nio.charset.MalformedInputException: Input length = 1"? UTF-8 output from PowerShell ERROR 1115 (42000): Unknown character set: 'utf8mb4' "for line in..." results in UnicodeDecodeError: 'utf-8' codec can't decode byte How to make php display \t \n as tab and new line instead of characters