Java List to JSON array using Jackson with UTF-8 encoding
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23590475/
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
Java List to JSON array using Hymanson with UTF-8 encoding
提问by Mingoo
Now I'm trying to convert Java List object to JSON array, and struggling to convert UTF-8 strings. I've tried all followings, but none of them works.
现在我正在尝试将 Java List 对象转换为 JSON 数组,并且正在努力转换 UTF-8 字符串。我已经尝试了以下所有方法,但没有一个有效。
Settings.
设置。
response.setContentType("application/json");
PrintWriter out = response.getWriter();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
final ObjectMapper mapper = new ObjectMapper();
Test#1.
测试#1。
// Using writeValueAsString
String json = ow.writeValueAsString(list2);
Test#2.
测试#2。
// Using Bytes
final byte[] data = mapper.writeValueAsBytes(list2);
String json = new String(data, "UTF-8");
Test#3.
测试#3。
// Using ByteArrayOutputStream with new String()
final OutputStream os = new ByteArrayOutputStream();
mapper.writeValue(os, list2);
final byte[] data = ((ByteArrayOutputStream) os).toByteArray();
String json = new String(data, "UTF-8");
Test#4.
测试#4。
// Using ByteArrayOutputStream
final OutputStream os = new ByteArrayOutputStream();
mapper.writeValue(os, list2);
String json = ((ByteArrayOutputStream) os).toString("UTF-8");
Test#5.
测试#5。
// Using writeValueAsString
String json = mapper.writeValueAsString(list2);
Test#6.
测试#6。
// Using writeValue
mapper.writeValue(out, list2);
Like I said, none of above works. All displays characters like "???". I appreciate your helps. I'm using Servlet to send JSON response to clients.
就像我说的,以上都不起作用。全部显示“???”等字符。我很感激你的帮助。我正在使用 Servlet 向客户端发送 JSON 响应。
This problem only happens when I write java.util.List object. If I write single data object, e.g. customer object in below example, then there is no ??? characters, and UTF-8 is working with the following code.
这个问题只发生在我写 java.util.List 对象时。如果我编写单个数据对象,例如下面示例中的客户对象,则没有 ??? 字符,UTF-8 正在使用以下代码。
PrintWriter out = response.getWriter();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(customer);
out.print(json);
回答by Mingoo
The answer was very simple. You need to specify UTF-8 charset encoding in response.setContentType too.
答案很简单。您还需要在 response.setContentType 中指定 UTF-8 字符集编码。
response.setContentType("application/json;charset=UTF-8");
Then, many of above code will work correctly. I will leave my question as is, since it will show you several ways of writing JSON to clients.
然后,上面的许多代码都可以正常工作。我将保留我的问题,因为它将向您展示将 JSON 写入客户端的几种方法。
回答by ?smail Yavuz
On RequestMapping in Controller:
关于控制器中的 RequestMapping:
@RequestMapping(value = "/user/get/sth",
method = RequestMethod.GET,
produces = { "application/json;**charset=UTF-8**" })

