JSON字符编码 [英] JSON character encoding
问题描述
我的Java Web应用程式提交可传回JSON的AJAX要求,例如:
{'value':'aériennes'}
当'aériennes'显示在网页中时,它显示为'aririennes',因此我猜有一些字符编码的问题。 AJAX响应头包括
Content-Type应用程序/ json
它似乎不包括任何字符集信息。我想这需要改变成像
Content-Type text / html; charset = iso-8859-1(或charset = utf8)
应用程序的服务器端Spring MVC,我想必须有一种方法来为每个响应设置默认字符集。
症状表明,最初以UTF-8编码的JSON字符串已使用ISO-8859-1编码写入HTTP响应,并指示webbrowser将其显示为UTF-8。如果它是使用UTF-8编写的并显示为ISO-8859-1,那么你会看到aériennes
。如果它是使用ISO-8859-1显示的和,那么你会看到aïririennes
。
要解决JSON字符串不正确地写为ISO-8859-1的问题,您需要配置您的webapp / Spring以使用UTF-8作为HTTP响应编码。
response.setCharacterEncoding(UTF- 8);不要更改内容类型标题。这对JSON是完全正确的,它显示为UTF-8。
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?
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.
这篇关于JSON字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!