将 Latin-1 ( ISO-8859-1) 转义为 Java 中的 utf-8 字符串
Escape Latin-1 ( ISO-8859-1) to utf-8 String in Java
我也尝试过使用 Gson and org.json as the examples. I tried Commons Text,但是当我手动导入库时我不适合我(我不允许使用 Maven)。所以我决定寻找另一种解决方案。
NoClassDefFoundError: org/apache/commons/text/StringEscapeUtils
我需要通过这种方式将数组转义为 Json。特别是 Ó
、ó
或任何 Latin-1 字符(不转义 "
,只是转义 "&%$/"Helló"
中的内容)。原始消息:Helló / // \ "WÓRLD"
{"token":"045-245","message":"Helló / // \ \"WÓRLD\" "}
至
{"token":"045-245","message":"Hell\u00F3 / // \ \"W\u00D3RLD\" "}
这是我使用时得到的:
Gson
JsonObject json = new JsonObject();
json.addProperty("token", "045-245");
json.addProperty("message", "Helló WÓRLD");
String payload = new Gson().toJson(json);
System.out.println(payload);
结果:
{"token":"045-245","message":"Helló WÓRLD"}
org.json
JSONObject jsonObject = new JSONObject();
jsonObject.put("token", "045-245");
jsonObject.put("message", "Helló WÓRLD");
System.out.println(jsonObject.toString());
结果:
{"message":"Helló WÓRLD","token":"045-245"}
感谢@dnault,我找到了 Jackson Json 图书馆。我需要导入 com.fasterxml.jackson.core
、com.fasterxml.jackson.databind
。我也导入了 com.fasterxml.jackson.annotations
,在 JDK 8.
上没有问题
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class Foo {
ObjectMapper mapper = new ObjectMapper();
mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
ObjectNode node = mapper.getNodeFactory().objectNode();
node.put("token", "045-245");
node.put("message", "Helló / // \ \"WÓRLD\" ");
System.out.println(mapper.writeValueAsString(node));
}
输出:
{"token":"045-245","message":"Hell\u00F3 / // \ \"W\u00D3RLD\" "}
我也尝试过使用 Gson and org.json as the examples. I tried Commons Text,但是当我手动导入库时我不适合我(我不允许使用 Maven)。所以我决定寻找另一种解决方案。
NoClassDefFoundError: org/apache/commons/text/StringEscapeUtils
我需要通过这种方式将数组转义为 Json。特别是 Ó
、ó
或任何 Latin-1 字符(不转义 "
,只是转义 "&%$/"Helló"
中的内容)。原始消息:Helló / // \ "WÓRLD"
{"token":"045-245","message":"Helló / // \ \"WÓRLD\" "}
至
{"token":"045-245","message":"Hell\u00F3 / // \ \"W\u00D3RLD\" "}
这是我使用时得到的:
Gson
JsonObject json = new JsonObject();
json.addProperty("token", "045-245");
json.addProperty("message", "Helló WÓRLD");
String payload = new Gson().toJson(json);
System.out.println(payload);
结果:
{"token":"045-245","message":"Helló WÓRLD"}
org.json
JSONObject jsonObject = new JSONObject();
jsonObject.put("token", "045-245");
jsonObject.put("message", "Helló WÓRLD");
System.out.println(jsonObject.toString());
结果:
{"message":"Helló WÓRLD","token":"045-245"}
感谢@dnault,我找到了 Jackson Json 图书馆。我需要导入 com.fasterxml.jackson.core
、com.fasterxml.jackson.databind
。我也导入了 com.fasterxml.jackson.annotations
,在 JDK 8.
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class Foo {
ObjectMapper mapper = new ObjectMapper();
mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
ObjectNode node = mapper.getNodeFactory().objectNode();
node.put("token", "045-245");
node.put("message", "Helló / // \ \"WÓRLD\" ");
System.out.println(mapper.writeValueAsString(node));
}
输出:
{"token":"045-245","message":"Hell\u00F3 / // \ \"W\u00D3RLD\" "}