public static JSONObject formatStr( String unicodeJsonString){
// 使用 Gson 将 Unicode 编码转换为 UTF-8 编码
JsonElement jsonElement = JsonParser.parseString(unicodeJsonString);
String utf8JsonString = new Gson().toJson(jsonElement);
// 从 UTF-8 JSON 字符串中提取数据
JsonObject jsonObject = jsonElement.getAsJsonObject();
// String name = jsonObject.get("name").getAsString();
//
// System.out.println("UTF-8 Name: " + name);
// 将 Gson 的 JsonObject 转换为 Fastjson 的 JSON 对象
JSONObject fastjsonObject = JSONObject.parseObject(jsonObject.toString());
return fastjsonObject;
}
Gson解决json字符串中包含unicode编码的value值时,不能转换JSONObject
本文介绍了如何在Java中使用Gson库将Unicode编码的JSON字符串转换为UTF-8编码,并进一步解析为JsonObject,最后将其转换为Fastjson的JSONObject对象。
摘要由CSDN通过智能技术生成