一个txt文件,内部的json格式如下:
{ "名称": "#pagecontainer > div.main > div.app-msg > div.app-detail > div > h1", "大小": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(3)", "更新时间": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(4)", "版本": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(5)", "格式": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(6)", "系统要求": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(8)", "权限要求": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > div.permissionsDetail.has-border.hide > ul", "好评人数": "#pagecontainer > div.main > div.app-msg > div.app-detail > div > span", "介绍": "#pagecontainer > div.main > div.detail-app-intro > div.main-left > div:nth-child(2) > p:nth-child(4)", "版本更新": "#pagecontainer > div.main > div.detail-app-intro > div.main-left > div:nth-child(2) > p:nth-child(6)", "用户评论": "#pagecontainer > div.main > div.detail-app-intro > div.main-left > div:nth-child(2) > ul" } { "名称": "#pagecontainer > div.main > div.app-msg > div.app-detail > div > h1", "大小": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(3)", "更新时间": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(4)", "版本": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(5)", "格式": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(6)", "系统要求": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(8)", "权限要求": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > div.permissionsDetail.has-border.hide > ul", "好评人数": "#pagecontainer > div.main > div.app-msg > div.app-detail > div > span", "介绍": "#pagecontainer > div.main > div.detail-app-intro > div.main-left > div:nth-child(2) > p:nth-child(4)", "版本更新": "#pagecontainer > div.main > div.detail-app-intro > div.main-left > div:nth-child(2) > p:nth-child(6)", "用户评论": "#pagecontainer > div.main > div.detail-app-intro > div.main-left > div:nth-child(2) > ul" } { "名称": "#pagecontainer > div.main > div.app-msg > div.app-detail > div > h1", "大小": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(3)", "更新时间": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(4)", "版本": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(5)", "格式": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(6)", "系统要求": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > p:nth-child(8)", "权限要求": "#pagecontainer > div.main > div.detail-app-intro > div.main-right > div:nth-child(1) > div > div.permissionsDetail.has-border.hide > ul", "好评人数": "#pagecontainer > div.main > div.app-msg > div.app-detail > div > span", "介绍": "#pagecontainer > div.main > div.detail-app-intro > div.main-left > div:nth-child(2) > p:nth-child(4)", "版本更新": "#pagecontainer > div.main > div.detail-app-intro > div.main-left > div:nth-child(2) > p:nth-child(6)", "用户评论": "#pagecontainer > div.main > div.detail-app-intro > div.main-left > div:nth-child(2) > ul" }
使用json工具类很难直接处理,这个时候可以利用字符串的方式:
private static List<String> getContentFromParenthese(String content) { ArrayList<String> contents = new ArrayList<String>(); int m = 0, n = 0; int count = 0; for (int i = 0; i < content.length(); i++) { if (content.charAt(i) == '{') { if (count == 0) { m = i; } count++; } if (content.charAt(i) == '}') { count--; if (count == 0) { n = i; contents.add(content.substring(m, n + 1)); } } } return contents; }
或者使用数组的方式:
private static List<String> getContentByArray(String content) { ArrayList<String> contents = new ArrayList<String>(); List<Character> segment=new ArrayList<Character>(); int count = 0; for(int i=0;i<content.length();i++){ System.out.println(content.charAt(i)); if (content.charAt(i) == '{') { count++; } if (content.charAt(i) == '}') { count--; } if(count>0){ segment.add(content.charAt(i)); } if(count==0&&segment.size()>0){ StringBuilder str = new StringBuilder(); for (Character character : segment) {// 对ArrayList进行遍历,将字符放入StringBuilder中 str.append(character); } contents.add(str.toString()); segment=new ArrayList<Character>(); } } return contents; }