Java JSON 字符编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3995559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON character encoding
提问by Dónal
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. The AJAX response headers include
当网页中显示'aériennes'时,它显示为'a?riennes',所以我猜是某种字符编码问题。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,我想必须有一种方法可以为每个响应设置默认字符集?
回答by Thanatos
First, your posted data isn't valid JSON. This would be:
首先,您发布的数据不是有效的 JSON。这将是:
{"value": "aériennes"}
Note the double quotes: They are required.
注意双引号:它们是必需的。
The Content-Type for JSON data should beapplication/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 数据的 Content-Type应该是application/json
. 实际的 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 é. I've never worked with Spring, but if it's doing the JSON encoding, this is probably taken care of properly, for you.
您可以使用 Wireshark 之类的工具来监控网络流量并查看数据的外观,您应该看到c3 89
é的字节数。我从来没有使用过 Spring,但是如果它正在执行 JSON 编码,这可能会为您妥善处理。
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?
一旦 JSON 到达浏览器,它应该很好,如果它是有效的。但是,您如何将 JSON 响应中的数据插入到网页中?
回答by BalusC
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 anddisplayed using ISO-8859-1, then you would have seen a???riennes
.
症状表明,最初采用 UTF-8 编码的 JSON 字符串使用 ISO-8859-1 编码写入 HTTP 响应,并指示网络浏览器将其显示为 UTF-8。如果它是使用 UTF-8 编写并显示为 ISO-8859-1,那么您会看到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. Basically, it should be doing the following under the covers:
要解决 JSON 字符串错误地写为 ISO-8859-1 的问题,您需要配置您的 webapp/Spring 以使用 UTF-8 作为 HTTP 响应编码。基本上,它应该在幕后做以下事情:
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。
回答by Jorge Carretero
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.
到您的内容类型。
回答by ton
response.setContentType("application/json;charset=utf-8");
回答by Lars Juel Jensen
I don′t know if this is relevant anymore, but I fixed it with the @RequestMapping annotation.
我不知道这是否相关,但我用 @RequestMapping 注释修复了它。
@RequestMapping(method=RequestMethod.GET, produces={"application/json; charset=UTF-8"})
回答by craned
回答by AbdulHayee
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.
这肯定会有所帮助。
回答by Yuri Gavrysh
Also, you can use spring annotation RequestMappingabove controller class for receveing application/json;utf-8in all responses
此外,您可以在控制器类上方使用 spring 注释RequestMapping来接收所有响应中的application/json;utf-8
@Controller
@RequestMapping(produces = {"application/json; charset=UTF-8","*/*;charset=UTF-8"})
public class MyController{
...
}
回答by Erando
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.
我的问题是我使用 Springs 在响应中返回了一个 json 字符串@ResponseBody
。如果您也这样做,这可能会有所帮助。
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)
回答by Alexandre Pimentel
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 :
这里的答案帮助我解决了我的问题,尽管它并不完全相关。我使用 javax.ws.rs API 以及 @Produces 和 @Consumes 注释并遇到了同样的问题 - 我在网络服务中返回的 JSON 不是 UTF-8。我在我的控制器功能之上使用以下注释解决了它:
@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.
在每个端点的 get 和 post 函数上。我没有设置字符集,这解决了它。这是球衣的一部分,所以也许您必须添加一个 maven 依赖项。