json中含有特殊字符串如何处理

当遇到JSON字符串包含特殊字符时,可以使用谷歌的Gson库进行转换。通过创建JsonParser对象解析字符串,然后用Gson再次转换为对象,确保数据正确处理。示例代码展示了如何将JSON字符串转换为TbspResponse对象并回转为JSON。

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

可以使用谷歌的JsonParser 转换一下,实例代码如下:

public class JsonTest {

    public static void main(String[] args){
        String ss = "{\"headTrCode\":\"030500D600\",\"headUserId\":\"batch_user_id\",\"headCustNo\":\"batch_cust_no\",\"headReqNo\":\"844696e2cf244cae969697f62270a8ca\",\"headSerialNo\":\"201705150000006949\",\"headTenantId\":\"004\",\"headOrgNo\":\"batch_org_no\",\"headTrDate\":\"20170515\",\"headReqTrDate\":\"20170704\",\"headTrTime\":\"162019\",\"headChannel\":\"06\",\"language\":\"ZH\",\"respcode\":\"0TB320000005\",\"respmsg\":\"请求参数校验失败[对账日期不能为空]\",\"responseType\":\"FAIL\",\"respJnl\":\"FAIL\"}";

        TbspResponse response = new Gson().fromJson(ss,TbspResponse.class);
        System.out.println(response.getHeadChannel());
        String tt  = new Gson().toJson(response);
        System.out.println(tt);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser parser = new JsonParser();
        JsonElement je = parser.parse(tt.toString());
        String result = gson.toJson(je);
        TbspResponse response2 = new Gson().fromJson(ss,TbspResponse.class);
        System.out.println(response.getHeadChannel());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值