如何确保在Java中将JSON字符串编码为UTF-8。

huangapple go评论70阅读模式
英文:

How to ensure that the JSON string is UTF-8 encoded in Java

问题

我正在处理一个遗留的 Web 服务客户端代码,其中 JSON 数据被发送到该 Web 服务。最近发现,对于 JSON 主体中的某些请求,由于 JSON 主体中存在无效字符(非 UTF-8 编码),该服务会返回 HTTP 400 响应。

以下是导致问题的数据示例:

String value = "zu3z5eq tô‰U\f‹Á‹€z";

我正在使用 org.json.JSONObject.toString() 方法生成 JSON 字符串。请问如何确保 JSON 字符串是 UTF-8 编码的?

我已经尝试了一些在线上可用的解决方案,比如将其转换为字节数组然后再转换回来,使用 Java 的字符集方法等,但它们都没有起作用。要么会转换有效的值,比如中文/日文字符,要么根本不起作用。

您能否提供一些关于这个问题的建议?

英文:

I am working on a legacy web service client code where the JSON data is being sent to the web service. Recently it was found that for some requests in the JSON body, the service is giving HTTP 400 response due to invalid characters (non-UTF8) in the JSON Body.

Below is one example of the data which is causing the issue.

String value = "zu3z5eq tô‰U\f‹Á‹€z";

I am using org.json.JSONObject.toString() method to generate the JSON string. Can you please let me know how can I ensure that the JSON string is UTF-8 encoded?

I already tried few solutions like available online , like converting to byte array and then back, using java charset methods etc, but they did not work. Either they convert the valid values as well like chinese/japanese characters, or doesn't work at all.

Can you please provide some input on this?

答案1

得分: 0

你在创建OutputStreamWriter时需要设置字符编码:

 httpConn.connect();
 wr = new OutputStreamWriter(httpConn.getOutputStream(), StandardCharsets.UTF_8); 
 wr.write(jsonObject.toString());
 wr.flush();

否则它会默认使用“平台默认编码”,这是一种在你所运行的系统上历史上用于文本文件的编码方式。

英文:

You need to set the character encoding for OutputStreamWriter when you create it:

 httpConn.connect();
 wr = new OutputStreamWriter(httpConn.getOutputStream(), StandardCharsets.UTF_8); 
 wr.write(jsonObject.toString());
 wr.flush();

Otherwise it defaults to the "platform default encoding," which is some encoding that has been used historically for text files on whatever system you are running.

答案2

得分: -1

以下是翻译好的内容:

使用 Base64 编码将值转换为 Byte[]。

String value = "zu3z5eq tô‰U\f‹Á‹€z";

// 在发送时进行编码
byte[] encodedBytes = Base64.getEncoder().encode(value.getBytes("UTF-8"));
String encodedValue = new String(encodedBytes, "UTF-8");

// 传输过程...

// 在接收端进行解码
byte[] decodedBytes = Base64.getDecoder().decode(encodedValue.getBytes("UTF-8"));
System.out.println(new String(decodedBytes, "UTF-8"));
英文:

Use Base64 encoding for converting the value to Byte[].

  String value = "zu3z5eq tô‰U\f‹Á‹€z";
	
	// WHILE  SENDING  ENCODE THE VALUE
	byte[] encodedBytes = Base64.getEncoder().encode(value.getBytes("UTF-8"));
	String encodedValue = new String(encodedBytes, "UTF-8");
	
	// TRANSPORT....
	
	// ON RECEIVING END DECODE THE  VALUE
	byte[] decodedBytes = Base64.getDecoder().decode(encodedValue.getBytes("UTF-8"));
	System.out.println( new  String(decodedBytes, "UTF-8"));

huangapple
  • 本文由 发表于 2020年4月8日 23:43:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61104629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定