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){
    }
    }
    }
    }
  • 相关阅读:
    atitit...触发器机制 ltrigger mechanism sumup .的总结O8f
    atitit. 集合groupby 的实现(2)---自定义linq查询--java .net php
    atitit. groupby linq的实现(1)-----linq框架选型 java .net php
    atitit.j2ee 1.5 1.6 的不同跟 Servlet 3.0新特性总结
    Atitit. 常用街机系统and 模拟器总结 snk neo geo cps mame sfc smc
    atitit. access token是什么??微信平台公众号开发access_token and Web session保持状态机制
    atitit.二进制数据无损转字符串网络传输
    atitit.压缩算法 ZLib ,gzip ,zip 最佳实践 java .net php
    Atitit.现实生活中最好使用的排序方法-----ati排序法总结
    atitit.修复xp 操作系统--重装系统--保留原来文件不丢失
  • 原文地址:https://www.cnblogs.com/wind-man/p/12509200.html
Copyright © 2011-2022 走看看