简体   繁体   English

JSON字符编码

[英]JSON character encoding

My Java web application submits an AJAX request that returns JSON such: 我的Java Web应用程序提交了一个返回JSON的AJAX请求:

{'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. 当'aériennes'显示在网页上时,它显示为'a riennes',所以我猜有某种字符编码问题。 The AJAX response headers include AJAX响应头包括

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? 应用程序的服务器端是Spring MVC,我想必须有一种方法为每个响应设置默认字符集?

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. 这些症状表明最初采用UTF-8编码的JSON字符串使用ISO-8859-1编码写入HTTP响应,并指示webbrowser将其显示为UTF-8。 If it was written using UTF-8 and displayed as ISO-8859-1, then you would have seen aériennes . 如果它是使用UTF-8编写并显示为ISO-8859-1,那么你会看到aériennes If it was written and displayed using ISO-8859-1, then you would have seen a�riennes . 如果它是使用ISO-8859-1编写显示的,那么你会看到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. 要解决JSON字符串错误地写为ISO-8859-1的问题,您需要将webapp / Spring配置为使用UTF-8作为HTTP响应编码。 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. 这对于JSON来说非常好,它被显示为UTF-8。

我不知道这是否相关,但我用@RequestMapping注释修复了它。

@RequestMapping(method=RequestMethod.GET, produces={"application/json; charset=UTF-8"})

First, your posted data isn't valid JSON. 首先,您发布的数据无效JSON。 This would be: 这将是:

{"value": "aériennes"}

Note the double quotes: They are required. 请注意双引号:它们是必需的。

The Content-Type for JSON data should be application/json . JSON数据的Content-Type 应为 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. 实际的JSON数据(我们上面提到的)应该使用UTF-8,UTF-16或UTF-32进行编码 - 我建议使用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 é. 您可以使用Wireshark之​​类的工具来监控网络流量并查看数据的外观,您应该看到é的字节c3 89 I've never worked with Spring, but if it's doing the JSON encoding, this is probably taken care of properly, for you. 我从来没有使用过Spring,但是如果它正在进行JSON编码,那么对你来说这可能是正确的。

Once the JSON reaches the browser, it should good, if it is valid. 一旦JSON到达浏览器,它应该是好的,如果它是有效的。 However, how are you inserting the data from the JSON response into the webpage? 但是,如何将JSON响应中的数据插入到网页中?

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. 这肯定会有所帮助。

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. 到您的contentType。

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

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. 我使用javax.ws.rs API和@Produces和@Consumes注释并遇到同样的问题 - 我在webservice中返回的JSON不是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. 在每个端点的get和post函数上。 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. 这是球衣的一部分,所以也许你必须添加一个maven依赖。

If you're using StringEntity try this , using your choice of character encoding. 如果您使用StringEntity尝试这个 ,使用的字符编码的选择。 It handles foreign characters as well. 它也处理外来字符。

Also, you can use spring annotation RequestMapping above controller class for receveing application/json;utf-8 in all responses 此外,您可以使用控制器类上方的Spring注释RequestMapping来接收application / json; utf-8在所有响应中

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

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 . 我的问题是我使用Springs @ResponseBody在我的响应中返回了一个json字符串。 If you're doing this as well this might help. 如果你这样做也可能有所帮助。

Add the following bean to your dispatcher servlet. 将以下bean添加到调度程序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 ) (在此处找到: http//forum.spring.io/forum/spring-projects/web/74209-responsebody-and-utf-8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM