java之json去掉转义字符

时间:2024-10-30 13:05:11
{
    "jdytrip_products_detail_response": {
        ......
       "feature": "{\"title\":\"线路特色\",\"detail\":\"<p><span style=\\\"color: rgb(51, 51, 51); font-family: arial, 微软雅黑, sans-serif; font-size: 14px; line-height: 24px;\\\">临安因为爱情主题酒店临安因为爱情主题酒店位于河桥镇*向前100米,房间配套设施齐全,交通便利,环境优雅,周边有柳溪江女人河、风情小镇、唐昌老街、温泉、瑞晶石花洞等旅游景点,因为爱情主题酒店以热情的服务期待你的光临!<\\/span><\\/p>\"}", 
 ......
  }
}

存入数据库时不想让带上\ 转义,

......
JdytripProductsDetailResponse response = ropClient.execute(request, session);
                if (response != null && response.isSuccess()) {
                    ProductDetail productDetail = new ProductDetail();
                    productDetail.setProductId(Long.parseLong(response.getP_id()));
                    Map<String,Object> map = new HashMap<String, Object>();
                    if (StringUtils.isNotNullOrEmptyStr(response.getFeature())) {

                        map.put("feature",JSON.parse(response.getFeature()).toString());
//                        ("============="+(()));
                    }
                    if (StringUtils.isNotNullOrEmptyStr(response.getStroke())) {

                        map.put("stroke",JSON.parse(response.getStroke()));
                    }
                    if (StringUtils.isNotNullOrEmptyStr(response.getCost_in())) {

                        map.put("cost_in",JSON.parse(response.getCost_in()));
                    }
                    if (StringUtils.isNotNullOrEmptyStr(response.getCost_noin())) {

                        map.put("cost_noin",JSON.parse(response.getCost_noin()));
                    }
                    if (StringUtils.isNotNullOrEmptyStr(response.getNotice())) {

                        map.put("notice",JSON.parse(response.getNotice()));
                    }
                    productDetail.setContent(net.sf.json.JSONObject.fromObject(map).toString());
//                    ("================"+(map).toString().getBytes().length);
                    productDetail.setTitle(response.getP_name());
                    productDetail.setVersion("1.0");
                    productDetailList.add(productDetail);
                }
......

存入数据库的内容

{
    "feature": {
        "title": "线路特色",
        "detail": "<p>【真纯玩】销售冠军,直营团,不加水、不进店、无购物,不推销,&nbsp;<br/>【发团快】划片区直发,各片区独立成团,勿需中转换乘,最快发团,最早回程,游览时间多出1小时<br/>【包接】解除路不熟、打车难的苦恼,轻松到家<br/>【豪华土家宴】仙女山星级酒店提供“农家八大碗”,非农家乐用餐环境可比&nbsp;<br/>【导游好】亲妈般呵护、保姆级照顾,不催促、不赶路、配耳麦讲解&nbsp;<br/>【不换乘】专享绿色通道,天坑入口、地缝出口无需排队换车,减少舟车劳顿&nbsp;<br/>【赠品多】提供自拍杆、充电宝、矿泉水、雨衣(视天气)<br/>市场同类产品对比&nbsp;<br/>自营团 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 其它团<br/>【发团】 不换车,快发团√ &nbsp; &nbsp; &nbsp;需集散换车×&nbsp;<br/>【用餐】 豪华土家宴√ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;农家乐桌餐×&nbsp;<br/>【购物】 坚决不进店√ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;加水点购物×&nbsp;<br/>【导游】 好评导游耳麦讲解√ &nbsp; &nbsp;推销较多×</p><p><br/></p><p><br/></p>"
    },
    ......
       }
}

更新一个更简便的方式:

String s1="{\"MsgId\":1,\"TotalCount\":10,\"FilterCount\":8,\"SentCount\":7,\"ErrorCount\":1}";
System.out.println(StringEscapeUtils.unescapeJava(s1));

输出:

{"MsgId":1,"TotalCount":10,"FilterCount":8,"SentCount":7,"ErrorCount":1}