使用fastJson解析json字符串时出现反斜杠问题解决

本文探讨了在使用FastJson解析和重构JSON对象时,如何避免不必要的反斜杠转义字符。通过对比不同方法,如使用JSONPath.eval与直接调用getString方法,解释了为何在某些情况下会出现反斜杠,并提供了有效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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;

public class B64InterceptorTest {
    public static void main(String[] args) {
        //json字符串
        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);
        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.toString());
    }
}

//输出的值为
{"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"}}}

经过测试,如果单独输出字符串是不会有反斜杠的,只是将使用不同方法解析出来的字符串放入JSON对象中时,当值为json字符串时会出现反斜杠。
例如上面的

System.out.println(metaJson.getString("project")); // {"name":"zhangsan","age":"25"}
System.out.println(metaJson.getString("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"}}

从以上代码可以看到将json字符串解析出来再次放入json对象中时出现反斜杠,可以使用
JSONPath.eval(JSONObject对象,key)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值