尝试从 JSON 文件中读取更多记录,但只能读取其中的一部分 [已关闭]

trying to read more records from json file but able to read only part of it [closed]

提问人:zi zi 提问时间:11/11/2023 最后编辑:James Zzi zi 更新时间:11/11/2023 访问量:30

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答这个问题。

8天前关闭。

我是 Java 新手。我正在尝试读取包含 200 000 条记录的 json。从 API 调用中,我将每个响应获取 2 000 条记录作为分页(每页结果),在文件中仅读取前 2 000 条记录,而不从 json 文件中读取其他记录。如何让它从 json 文件中读取所有 200 000 条记录。我试着用谷歌搜索,但没有运气。你能告诉我如何解决它吗

@SuppressWarnings("unchecked")
   public void readjsonfile () throws JsonParseException, JsonMappingException, IOException, NullPointerException{
     ObjectMapper mapper = new ObjectMapper();
     mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
     File file = new File("../Animal_ID_Libraries_List.json");  ////Atchaya to remove the temp file
     File repofile = new File("../Tiger_Repo_List.json");
     File repofile_final = new File("../Tiger_Repo_Final_List.json");
     boolean ExceptionOccurred;
     
     AnimalIdLibsList AnimalIdLibs = new AnimalIdLibsList();
     try{
     AnimalIdLibs = mapper.readValue(file,  new TypeReference<AnimalIdLibsList>(){});
     }catch (JsonMappingException e){
     //AnimalIdLibs = mapper.readValue(file,  new TypeReference<AnimalIdLibsList>(){});  /// Atchaya to check for correct way of exception handling
     ExceptionOccurred = true;
     if (ExceptionOccurred = true){
       try {
         AnimalIdLibs = mapper.readValue(file, new TypeReference<AnimalIdLibsList>(){});
       }
       catch(JsonMappingException error){
         System.out.println("Exception occurred");
       }
     }
     }
     StringBuilder builder = new StringBuilder();
     List<Tigerjsonresponse> Tigerjsonresponse = new ArrayList<Tigerjsonresponse>();
     List<Tigerjsonresponse> Tigerjsonrespon = new ArrayList<Tigerjsonresponse>();
     try {
     ArrayList<AnimalVulnerability> AnimalIdLibVulnerabilities = AnimalIdLibs.vulnerabilities;
     HashMap<String, List<String>> AnimalAndBirdID = new HashMap<String, List<String>>();
     try {
     AnimalIdLibVulnerabilities.forEach(AnimalVul -> {
       List<String> BirdID = new ArrayList<String>();
       String AnimalID = AnimalVul.Animal.id;
       String status = AnimalVul.Animal.vulnStatus;
       String AnimalID_Status = AnimalVul.Animal.id +" " + AnimalVul.Animal.vulnStatus;
       ArrayList<AnimalNode> BirdNode = new ArrayList<AnimalNode>();
       try{
       BirdNode = AnimalVul.Animal.configurations.get(0).nodes;}
       catch(NullPointerException e) {
         System.out.println("NullPointerException thrown!");
       }
       BirdNode.forEach(node -> {
         ArrayList<AnimalBirdMatch> BirdMatch=node.BirdMatch;
         BirdMatch.forEach(match -> BirdID.add(match.criteria +":" + match.versionStartIncluding + ":" + match.versionEndIncluding));
       });
       AnimalAndBirdID.put(AnimalID_Status, BirdID);
     });}
     catch(NullPointerException e) {
       System.out.println("NullPointerException thrown! - Hashmap is not created");
     }
   
     
     AnimalAndBirdID.forEach((key, value) -> {
       Tigerjsonresponse Tigerjsonresp = new Tigerjsonresponse();
       String[] AnimalStatus = key.split("\\s+");
       List<String> Bird = Collections.synchronizedList(new ArrayList());
       Bird = value;
       //List<String> Bird = value;
       boolean catch_exception = false;
       //Bird.forEach(Birdstr -> {
       //for (int i_Bird=0; i_Bird<Bird.size(); i_Bird++){
       for (String Birdstr : Bird){
         //String[] Birdinit = Birdstr.split("2.3:a:");
         String[] Birdinit = Birdstr.split("2.3:a:");
         String[] BirdVersion = null;
         //try{
         BirdVersion = Birdinit[1].split(":");
         String version_start = null;
         String version_End = null;
         version_start = BirdVersion[10];
         version_End = BirdVersion[11];
         Tigerjsonresp.setComponentName(BirdVersion[0] + " " + BirdVersion[1]);
         Tigerjsonresp.setVersion((BirdVersion[2] + " " + BirdVersion[3]).replace("*", ""));
         Tigerjsonresp.setBirdID(Birdstr);
         Tigerjsonresp.setAnimalID(AnimalStatus[0]);
         Tigerjsonresp.setVulnStatus(AnimalStatus[1]);
         Tigerjsonresp.setVersionStartIncluding(version_start);
         Tigerjsonresp.setVersionEndIncluding(version_End);
         Tigerjsonresponse.add(Tigerjsonresp);
    
         }
       
     });
     System.out.println(Tigerjsonresponse);
     Set<Tigerjsonresponse> TigerUniqueSet= new HashSet<Tigerjsonresponse>();
     TigerUniqueSet.addAll(Tigerjsonresponse);         
       Tigerjsonrespon = new ArrayList<Tigerjsonresponse>();
       Tigerjsonrespon.addAll(TigerUniqueSet);  
     try {
       FileOutputStream writeData = new FileOutputStream(repofile, true);
       ObjectOutputStream writeStream = new ObjectOutputStream(writeData);
       writeStream.writeObject(Tigerjsonrespon);
       writeStream.close();
     } catch (IOException e) {
         System.out.println("Unable to write out names");
     }
     }
     catch (ArrayIndexOutOfBoundsException exception) {
       System.out.println("ArrayIndexOutOfBoundsException thrown! - unable to write content");
     }
     catch (Exception e1) {
       e1.printStackTrace();
     }
   }
java json spring-boot arraylist

评论

0赞 tgdavies 11/11/2023
请准确解释您的计划目前提供的结果,以及您的期望。
0赞 Ilya Lisov 11/11/2023
在此示例中,您没有提供 200000 或 2000 个数字,如何实现分页?此外,似乎还有一些与JVM相关的麻烦,因为200k对象的初始化可能会溢出ram限制

答: 暂无答案