解析json串,利用正则表达式,split



public class SplitJson {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
     String str = "[{\"version\":\"100\"},{\"type\":\"210\"},{\"chanl_no\":\"30\"},{\"chanl_sub_no\":\"3001\"},{\"chanl_date\":\"\"},{\"chanl_time\":\"\"},{\"ectip_date\":\"\"},{\"chanl_flow_no\":\"\"},{\"ectip_flow_no\":\"\"},{\"chanl_trad_no\":\"3FC012\"},{\"term_inf\":\"\"},{\"resp_code\":\"\"},{\"resp_msg\":\"\"},{\"page\":\"\"},{\"maxrow\":\"\"},{\"locstr\":\"\"},{\"tot_rec\":\"\"},{\"ARRAY_3FC012\":\"\"},{\"0\":\"\"},{\"age\":\"23\"},{\"name\":\"小cn\"},{\"sex\":\"男\"},{\"1\":\"\"},{\"sex\":\"女\"},{\"age\":\"24\"},{\"name\":\"小王\"},{\"29\":\"\"},{\"name\":\"小张\"},{\"age\":\"22\"},{\"sex\":\"男\"}]";
     //正则表达式的串为{\"29\":\"\"}
     String[] splitStr = str.split("\\{\\\"\\d{1,2}\\\":\\\"\\\"\\}");
     String resultStr = splitStr[0];
     for(int i = 1; i < splitStr.length; i++){
         resultStr += splitStr[i].replaceAll("\\},\\{", ",").substring(1);//去掉多余逗号
     }
     System.out.println(str);
     System.out.println(resultStr);
     //测试空指针异常
     System.out.println(spitJson(null, null));
     System.out.println(spitJson("", null));
     System.out.println(spitJson("", ""));
    }
    
    public static String spitJson(String jsonStr, String regex){
        if(jsonStr == null || regex == null ) return "";
        String[] splitStr = jsonStr.split(regex);
        String resultStr = splitStr[0];
        
        return resultStr;
    }

}

转载于:https://www.cnblogs.com/herosoft/p/5720632.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值