• 组合的json文件分隔或者拆分


    一个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;
        }
  • 相关阅读:
    壁纸网站收藏
    LaTeX公式学习
    最简单的音乐播放器,实现播放器基本功能
    计算机视觉领域的牛人博客、研究机构、博客
    视频压缩编码和音频压缩编码的基本原理
    MPEG-4 压缩编码标准
    视频编码标准简介
    视频压缩编码的基本原理
    数字视频原理
    视频压缩编码综述
  • 原文地址:https://www.cnblogs.com/davidwang456/p/8718619.html
Copyright © 2020-2023  润新知