fastJson字符串出现反斜杠问题解决

it2025-10-07  5

使用fastJson导入的pom依赖

<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.68</version> <scope>compile</scope> </dependency>

代码展示:

import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONPath; import org.apache.commons.lang.StringEscapeUtils; /** * @Desc * @Author madengling * @Time 2020/10/22 10:48 */ public class jsonTest { public static void main(String[] args) { String str = "{\"ctime\":\"20200518\",\"project\":{\"name\":\"zhangsan\",\"age\":\"25\"},\"content\":{\"distinct_id\":\"51818968\",\"event\":\"AppClick\",\"properties\":{\"element_page\":\"新闻列表页\",\"screen_width\":\"640\",\"app_version\":\"1.0\",\"os\":\"GNU/Linux\",\"battery_level\":\"11\",\"device_id\":\"886113235750\",\"client_time\":\"2020-05-18 13:53:56\",\"ip\":\"61.233.88.41\",\"is_charging\":\"1\",\"manufacturer\":\"Apple\",\"carrier\":\"中国电信\",\"screen_height\":\"320\",\"imei\":\"886113235750\",\"model\":\"\",\"network_type\":\"WIFI\",\"element_name\":\"tag\"}}}\n"; //将json字符串解析为json对象 JSONObject metaJson = JSONObject.parseObject(str); System.out.println("=====metaJson:"); System.out.println(metaJson); JSONObject jsonObject = new JSONObject(); //当值为单个字符串时,使用JSONObject对象.getString(key)方法,json对象输出时没有反斜杠 jsonObject.put("ctime", metaJson.getString("ctime")); //当值为json字符串时,使用JSONObject对象.getString(key)方法,json对象输出时带有反斜杠 jsonObject.put("project", metaJson.getString("project")); //当值为json字符串时,使用JSONPath.eval(JSONObject对象,key),json对象输出时没有反斜杠, jsonObject.put("content", JSONPath.eval(metaJson, "content")); System.out.println("=====jsonObject:"); System.out.println(jsonObject.toString()); JSONObject jsonObject1 = new JSONObject(); System.out.println("=====metaJson.getString(project):"); System.out.println(metaJson.getString("project")); String metaJsonString = metaJson.getString("project"); jsonObject1.put("propjectString",metaJsonString); JSONObject metaJsonJson = JSONObject.parseObject(metaJson.getString("project")); jsonObject1.put("propjectJson",metaJsonJson); JSONObject metaJsonJson1 = (JSONObject) metaJson.get("project"); jsonObject1.put("propjectJson1",metaJsonJson1); JSONObject metaJsonJson2 = metaJson.getJSONObject("project"); jsonObject1.put("propjectJson2",metaJsonJson2); Jtcyxx boy = new Jtcyxx(); boy.setGx(1); boy.setXm("张三"); boy.setYddh("196513"); jsonObject1.put("project", boy); System.out.println("=====jsonObject1:"); System.out.println(jsonObject1.toString()); System.out.println("=====tmp:"); String tmp = StringEscapeUtils.unescapeJavaScript(jsonObject.toString()); System.out.println(tmp); } }

经过测试,通过getString获取json后直接放在value位置时出现反斜杠。同时如果单独对这个json进行输出的话是没有反斜杠的,或者自定义一个对象放在value上也不会出现反斜杠现象。以及将这个对象通过除了getString之外的方法获取数据时,都不会出现反斜杠的现象。 注意:用get方法强转成(JSONObject)在没有getJSONObject()情况下正常显示,两个一起的时候会出现显示对象类的情况,暂时没找到原因,只能避免这种情况 执行结果: 遇到这种问题的时候,可以从两方面进行解决, 一是从源头生成结果的时候解决,避免getString结果放入json中产生反斜杠 二是处理产生的结果,可以用StringEscapeUtils.unescapeJavaScript(String str)方法处理掉反斜杠

我是因为调用别人的方法接收到参数即是如此,从网上找到的大多数都是用replaceAll("\"","")这样的方法,感觉不是很高大上,哈哈,百度了好久,找到了unescapeJavaScript的解决办法

参考文档: https://blog.csdn.net/programmer_trip/article/details/108350128 https://www.csdn.net/gather_21/Mtjacg2sODg5NDAtYmxvZwO0O0OO0O0O.html

最新回复(0)