Java在Map转Json字符串时出现"\"转义字符的解决办法
项目中出现了这样一个问题,当一个Map被转成Json字符串后,由于业务需求,会将Map转为JsonObject,添加元素后,又转成Json字符串,最后再被包装到另一个Map里,被转成Json字符串发送给APP端,这个时候APP端反应结果中带有“\”转义字符,无法解析。
简化一下问题
当一个Map被转成Json字符串后,被添加到另一个Map中,如果这个新的Map需要转成Json字符串格式,那么转化后,内部的这个Map转成的Json字符串,都会被加上“\”转义字符。
简单的模拟一下
public static void main(String[] args) { HashMap<String, Object> param = new HashMap<>(); param.put("userId", 66666); param.put("username", "XXXX"); HashMap<String, String> pushMap = new HashMap<>(); pushMap.put("testKey01", "value01"); pushMap.put("testKey02", "value02"); param.put("pushJson", JSON.toJSONString(pushMap)); String pushJson = JSON.toJSONString(param); System.out.println(pushJson); }
格式化一下结果:
{
"pushJson":
"{\"testKey01\":\"value01\",\"testKey02\":\"value02\"}",
"userId":66666,
"username":"XXXX"
}
延伸测试一下,如果用JSONObject转Json字符串会不会有问题:
public static void main(String[] args) { HashMap<String, Object> param = new HashMap<>(); param.put("userId", 66666); param.put("username", "XXXX"); // HashMap<String, String> pushMap = new HashMap<>(); // pushMap.put("testKey01", "value01"); // pushMap.put("testKey02", "value02"); JSONObject pushObject = new JSONObject(); pushObject.put("testKey01", "value01"); pushObject.put("testKey02", "value02"); param.put("pushJson", pushObject.toJSONString()); String pushJson = JSON.toJSONString(param); System.out.println(pushJson); }
格式化一下结果:
{
"pushJson":
"{\"testKey01\":\"value01\",\"testKey02\":\"value02\"}",
"userId":66666,
"username":"XXXX"
}
用原本的Map和JSONObject:
public static void main(String[] args) { HashMap<String, Object> param = new HashMap<>(); param.put("userId", 66666); param.put("username", "XXXX"); HashMap<String, String> pushMap = new HashMap<>(); pushMap.put("testKey01", "value01"); pushMap.put("testKey02", "value02"); JSONObject pushObject = new JSONObject(); pushObject.put("testKey01", "value01"); pushObject.put("testKey02", "value02"); param.put("pushJson", pushMap); String pushJson1 = JSON.toJSONString(param); System.out.println(pushJson1); param.put("pushJson", pushObject); String pushJson2 = JSON.toJSONString(param); System.out.println(pushJson2); }
结果:
{"pushJson":{"testKey01":"value01","testKey02":"value02"},"userId":66666,"username":"XXXX"}
{"pushJson":{"testKey01":"value01","testKey02":"value02"},"userId":66666,"username":"XXXX"}
这样就不带转义字符了
而项目中,我是采用将JSON字符串转回Map,然后添加到新的Map中,再转成JSON字符串的方式解决这个问题的:
HashMap<String, Object> pushApp = new HashMap<>(); pushApp.put("userId", userList); pushApp.put("pushType", pushType); pushApp.put("pushJson", JSON.parseObject(message, HashMap.class)); String pushJson = JSON.toJSONString(pushApp);
当然上面带“\”转义字符的结果也是有办法解析的:
public static void main(String[] args) { HashMap<String, Object> param = new HashMap<>(); param.put("userId", 66666); param.put("username", "XXXX"); HashMap<String, String> pushMap = new HashMap<>(); pushMap.put("testKey01", "value01"); pushMap.put("testKey02", "value02"); param.put("pushJson", JSON.toJSONString(pushMap)); String pushJson = JSON.toJSONString(param); System.out.println(pushJson); JSONObject jsonObject = JSONObject.parseObject(pushJson); JSONObject jsonData = jsonObject.getJSONObject("pushJson"); System.out.println("pushJson: " + jsonData); String value1 = jsonData.getString("testKey01"); System.out.println("key: testKey01 value: " + value1); }
结果:
{"pushJson":"{\"testKey01\":\"value01\",\"testKey02\":\"value02\"}","userId":66666,"username":"XXXX"}
pushJson: {"testKey01":"value01","testKey02":"value02"}
key: testKey01 value: value01
2019-09-12
HashMap jsonMap = JSON.parseObject(pushJson,HashMap.class); String resultJson = String.valueOf(jsonMap.get("pushJson")); System.out.println(resultJson);
反解析的时候,还是可以正常解析出来,不需要转成jsonObject也可以
{"pushJson":"{\"testKey01\":\"value01\",\"testKey02\":\"value02\"}","userId":66666,"username":"XXXX"}
{"testKey01":"value01","testKey02":"value02"}
到此这篇关于Java在Map转Json字符串时出现"\"转义字符的解决办法的文章就介绍到这了,更多相关Java Map转Json内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Java方法递归的形式和常见递归算法(方法递归结合File类查找文件)
方法递归方法直接调用自己或者间接调用自己的形式称为方法递归( recursion),递归做为一种算法在程序设计语言中广泛应用,这篇文章主要介绍了Java方法递归的形式和常见递归算法-方法递归结合File类查找文件,需要的朋友可以参考下2023-02-02
最新评论