首页 > Web开发 > 详细

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

时间:2016-07-30 13:26:55      阅读:734      评论:0      收藏:0      [点我收藏+]



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;
    }

}

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

原文:http://www.cnblogs.com/herosoft/p/5720632.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!