Search code examples
javajsonjsonparsersimplejson

Java reading json :reading data from json array which present inside json object


I want read json file using java . content of my json files are as below


{
  "ServiceData": {
    "ServiceInfo": [     
      {
        "trav": "20200131T173017Z",
        "real": "MyService",
        "lintruntime": "7",
        "upload": "184",
        "build": "1717",
        "buildproductruntime": "1709",
        "EXITCODE": "0",
        "totaltime": "3610"
      },
      {
        "trav": "20200131T173024Z",
        "real": "ERSampleService1",
        "lintruntime": "38",
        "upload": "381",
        "build": "1765",
        "buildproductruntime": "1767",
        "EXITCODE": "0",
        "totaltime": "3913"
      }
    ]
  }
}

How can do it using simple json I tried below but it's not working

public static void main(String args[])
    {
         try {
              Object obj = new JSONParser().parse(new FileReader("metricDataFinal.json"));  
              JSONObject jo = (JSONObject) obj; 
              Map ServiceData = ((Map)jo.get("ServiceData")); 
              Iterator<Map.Entry> itr1 = ServiceData.entrySet().iterator(); 
                while (itr1.hasNext()) { 
                     JSONArray ja = (JSONArray) jo.get("ServiceInfo"); 
                     Iterator itr2 = ja.iterator(); 

                     while (itr2.hasNext())  
                     { 
                         itr1 = ((Map) itr2.next()).entrySet().iterator(); 
                         while (itr1.hasNext()) { 
                             Map.Entry pair = itr1.next(); 
                             System.out.println(pair.getKey() + " : " + pair.getValue()); 
                         } 
                     } 
                    Map.Entry pair = itr1.next(); 
                   // System.out.println(pair.getKey() + " : " + pair.getValue()); 



                } 

         }catch(Exception e)
         {

         }
}


how to read data from for json I wanted to do processing on this data Please let me know if anyone knows how to do it


Solution

  • Try the code below:

    Object obj = new JSONParser().parse(new FileReader("metricDataFinal.json"));
                JSONObject jo = (JSONObject) obj;
                Map ServiceData = ((Map) jo.get("ServiceData"));
                Iterator<Map.Entry> itr1 = ServiceData.entrySet().iterator();
                while (itr1.hasNext()) {
                    JSONArray ja = (JSONArray) ServiceData.get("ServiceInfo");
                    Iterator itr2 = ja.iterator();
                    while (itr2.hasNext()) {
                        itr1 = ((Map) itr2.next()).entrySet().iterator();
                        while (itr1.hasNext()) {
                            Map.Entry pair = itr1.next();
                            System.out.println(pair.getKey() + " : " + pair.getValue());
                        }
                    }
                    Map.Entry pair = itr1.next();
                    // System.out.println(pair.getKey() + " : " + pair.getValue());
    
    
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    

    You have done a mistake in the iterator where you are trying to get ServiceInfo object. you are mistakenly trying to get that object from jo object but that object is in ServiceData object.