JSON文件beeing被读取两次,而不是在JAVA中读取一次

时间:2019-07-01 08:02:29

标签: java json

我正在构建一个连接器以读取JSON文件并写入数据库。我的问题是,它会将所有内容写入两倍,也就是说,如果有2个文件,它将读取两次,然后3次读取3个文件,等等。 *编辑了更多信息和将其写入数据库的部分。尽管如此,我认为问题出在我的许多for循环中,但无法解决。这几乎是我的完整代码:


public void collectData(File file, Map<String, String> args)
          throws FileNotFoundException, IOException,SQLException 
  {
// reading the JSON file
          File folder = new File(sourcepath);// where Inubit will save the files
          File[] fileNames = folder.listFiles();

          // go through all files
          for (File jsonInputFile : fileNames) {
              if (jsonInputFile.getName().toString().startsWith("Metadata_JSON3")) {
                  System.out.println("JSON File: " + jsonInputFile);
              } else {
                  System.out.println("Not correct JSON file" + jsonInputFile);
                  continue;
              } // filter non JSON Files

              // setting JSON parser
              JSONParser parser = new JSONParser();
              JSONParser parser2 = new JSONParser();
              FileReader reader = new FileReader(jsonInputFile);
              Object obj = null;
              try {
                  obj = parser.parse(reader);
              } catch (Exception e) {
              }
              try {
                  jsonarray = (JSONArray) obj;
              } catch (Exception p1) {
                  String objcast = "[" + obj + "]";

                  try {
                      jsonarray = (JSONArray) parser2.parse(objcast);
                  } catch (ParseException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
              // starting to read the JSON file
              for (Object valuesObj : jsonarray) {
                  JSONObject values2 = (JSONObject) valuesObj;

                  // getting points
                  JSONArray row = (JSONArray) values2.get("row");
                  for (int i = 0; i < row.size(); i++) {
                      JSONObject rows = (JSONObject) row.get(i);
                      Long Curve_id = (Long) rows.get("CURVE_ID");
                      String Curve_id_str = Long.toString(Curve_id);
                      ts_info_map.put("Curve_id_m", Curve_id_str);
                      // System.out.println("Curve_id is: " + Curve_id);
                      String Curve_name = (String) rows.get("CURVE_NAME");
                      ts_info_map.put("Curve_name_m", Curve_name);
                      String Value_frequency_is = (String) rows.get("META.Value.Frequency.is");
                      ts_info_map.put("Value_frequency_is_m", Value_frequency_is);
                      String Issue_frequency_is = (String) rows.get("META.Issue.Frequency.is");
                      ts_info_map.put("Issue_frequency_is_m", Issue_frequency_is);
                      String Timezone = (String) rows.get("META.Timezone.is");
                      ts_info_map.put("Timezone_m", Timezone);
                  } // third for loop
              }// second for loop

              try {
                  reader.close();
                  parser.reset();
                  // parser2.reset();
              } catch (Exception e) {
                  System.out.println("reader or parser cannot be closed");
                  // nothing... sometimes not possible to close reader
              }

          }// end of first for loop
      }

@Override
  public void collectInfo(File file, Map<String, String> args)
          throws FileNotFoundException, IOException {

      // TODO Auto-generated method stub
  }
  @Override
  public void refreshInfo(File file, Map<String, String> args)
          throws FileNotFoundException, IOException {
      // TODO Auto-generated method stub
      this.ts_info_map.clear();
      try {
          collectData(file, args);
      } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }

  @Override
  public void insertData(int runid, int subrunid, Map<String, String> args)
          throws SQLException {

      try {
      String datatable  = args.get("datatable");
      Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
      String sql = "insert into "
              + datatable
              + " (run_id,subrun_id,CURVE_ID, CURVE_NAME, VALUE_FREQUENCY_IS, ISSUE_FREQUENCY_IS, TIMEZONE_IS) values "
              + " (?" + StringUtils.repeat(",?", 7 - 1) + ")";

      PreparedStatement ps = con.prepareStatement(sql);

          // prepare sql statement 
          ps.setInt(1, runid);
          ps.setInt(2, subrunid);
          ps.setString(3, ts_info_map.get("Curve_id_m"));
          ps.setString(4,ts_info_map.get("Curve_name_m"));
          ps.setString(5,ts_info_map.get("Value_frequency_is_m"));
          ps.setString(6,ts_info_map.get("Issue_frequency_is_m"));
          ps.setString(7,ts_info_map.get("Timezone_m"));

ps.addBatch();

      ps.executeBatch(); //writes in table
      ps.close();
      con.close();

      // TODO Auto-generated method stub
      } catch (Exception e) {System.out.println("insert data exception" +e);}
  }


1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。我有一个主程序,该程序调用每个文件的收集数据部分,然后在收集数据部分中调用for循环,从而导致两次写入。所以我刚刚删除了循环:


for (File jsonInputFile : fileNames) {// just removed this line

            if (jsonInputFile.getName().toString().startsWith("Metadata_JSON3")) {
                System.out.println("JSON File: " + jsonInputFile);
            } else {
                System.out.println("Not correct JSON file" + jsonInputFile);
                continue;
            } // filter non JSON Files

            // setting JSON parser
            JSONParser parser = new JSONParser();
            JSONParser parser2 = new JSONParser();
            FileReader reader = new FileReader(jsonInputFile);
            Object obj = null;
            try {
                obj = parser.parse(reader);
            } catch (Exception e) {
            }
.....


//also used the hash map to write the data in

for (Integer i = 0; i < row.size(); i++) {
              HashMap<String, String> row_map = new HashMap<String, String>();

              JSONObject rows = (JSONObject) row.get(i);
              Long Curve_id = (Long) rows.get("CURVE_ID");
              String Curve_id_str = Long.toString(Curve_id);
              row_map.put("Curve_id_m", Curve_id_str);
              row_map.put("Curve_name_m", (String) rows.get("CURVE_NAME"));

....
//the rest remained more or less the same as it was before