zoukankan      html  css  js  c++  java
  • 使用Stack识别JSON,针对key不固定情况

    使用Stack对大JSON进行识别。基本功能完成,需要考虑特殊情况。

    public HashMap<String,JSONObject> analyseFileToPutJSONInMysql(String filepath){
    HashMap<String,JSONObject> rs = new HashMap<>();
    ParserConfig.getGlobalInstance().setAutoTypeSupport(true); // 用于解决 autoType is not support.的错误

    // 按行读取数据,文件是一个JSON数组,第一个'['以及最后一个']'需要忽略
    File readFile = new File(filepath);
    BufferedReader reader = null;
    Stack stack = new Stack(); //定义栈,用于将[]、{}压入栈中判断JSON起始结尾标志,栈中没有符号的时候,一个JSON结束
    try{
    reader = new BufferedReader(new FileReader(readFile));
    int line = 1;
    StringBuilder str = new StringBuilder(); // 拼接JSON字符串,用于解析JSON
    String tempString = reader.readLine(); // 获取文件第一行
    String tempStr = null; // 读取下一行,用于判断是否是末行
    while ( tempString != null){//BufferedReader有readLine(),可以实现按行读取
    tempString = this.dropEscapeFromString(tempString);
    // 跳过第一个[,
    if(line==1 && tempString.substring(1).length()==0 && tempString.substring(0,1).equalsIgnoreCase("[")) {
    str.append(tempString.substring(1));
    tempString = reader.readLine();
    str.append(tempString);
    continue;
    } //最后一个]处理
    if(str.toString().replace(" ","").equals("]")){
    tempString=null;
    continue;
    }else {
    stack = this.putInAndOutForJsonChar(tempString, stack);
    }
    // JSON的判断条件与结束位置
    if(stack.size()==0 && str.toString().replace(" ","").length()>1 ) {
    if(tempStr==null) {
    str.append(tempString);
    tempString = tempStr; //将 tempString置为空,退出循环
    }else{
    str.append(tempString.replace(",",""));
    }
    // System.out.println(str.toString().replace(" ",""));
    // 得到一个个的JSON 对象
    JSONObject jsonObject = JSONObject.parseObject(str.toString().replace(" ",""));
    rs.put(jsonObject.getString("@id"),jsonObject);
    // this.parseMedicalKnowledgeJSOn(jsonObject); //将所有的数据放入数据库中
    str = new StringBuilder("");
    tempString = reader.readLine();
    if(tempString!=null) {
    str.append(tempString);
    }

    line++;
    if(line%1000==0) {
    System.out.println(line);
    }
    }else {
    tempStr = reader.readLine();
    tempString = tempStr;
    tempString = this.dropEscapeFromString(tempString);
    // 不是特殊情况,中间数据只有一个]的情况
    if(tempString.replace(" ","").equalsIgnoreCase("]")){ //一行只有一个]
    tempStr = reader.readLine();
    if(tempStr==null) {
    tempString = tempString.substring(0, tempString.length() - 1);
    }else {
    str.append(tempString);
    stack = this.putInAndOutForJsonChar(tempString,stack);
    tempString=tempStr;
    }
    }else {
    str.append(tempString);
    }
    }
    }
    reader.close();
    return rs;
    }catch(IOException e){
    e.printStackTrace();
    return null;
    }finally{
    if(reader != null){
    try{
    reader.close();
    }catch(IOException e){
    }
    }
    }
    }
  • 相关阅读:
    SSH和SSL比较
    SSL虚拟主机安全方案
    https在电子邮件安全解决方案
    centos tomcat安装
    laravel 添加第三方扩展库
    laravel-1 安装.配置
    centos7.0 vsftp配置
    centos 日常操作指令
    centos redis 安装
    centos php 扩展安装
  • 原文地址:https://www.cnblogs.com/wind-man/p/12509200.html
Copyright © 2011-2022 走看看