Getting element value from jsonpath whose root is an array

五迷三道 提交于 2019-12-20 04:36:53

问题


I have a JSON response which has root as an array of 1 or more objects. I want to extract the value of one of the elements within each object.

Here is the JSON sample:

[  
   {  
      "od_pair":"7015400:8727100",
      "buckets":[  
         {  
            "bucket":"C00",
            "original":2,
            "available":2
         },
         {  
            "bucket":"A01",
            "original":76,
            "available":0
         },
         {  
            "bucket":"B01",
            "original":672,
            "available":480
         }
      ]
   },
   {  
      "od_pair":"7015400:8814001",
      "buckets":[  
         {  
            "bucket":"C00",
            "original":2,
            "available":2
         },
         {  
            "bucket":"A01",
            "original":40,
            "available":40
         },
         {  
            "bucket":"B01",
            "original":672,
            "available":672
         },
         {  
            "bucket":"B03",
            "original":632,
            "available":632
         },
         {  
            "bucket":"B05",
            "original":558,
            "available":558
         }
      ]
   }
]

I want to access the values of od_pair within each object.

I tried referring to the root array as $ but that did not help.

This is the code snippet I have written:

        List<Object> LegList = jsonPath.getList("$");
        int NoofLegs = LegList.size();
        System.out.println("No of legs :" +NoofLegs);
        for (int j=0; j<=NoofLegs;j++) {
            String OD_Pair = jsonPath.param("j", j).getString("[j].od_pair");
            System.out.println("OD Pair: " + OD_Pair);
            List<Object> BucketsList = jsonPath.param("j", j).getList("[j].buckets");

            int NoOfBuckets = BucketsList.size();
            System.out.println("no of Buckets: " + NoOfBuckets);
        }

This is the error that I see:

Caused by: 
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup 
failed:
Script1.groovy: 1: unexpected token: [ @ line 1, column 27.
restAssuredJsonRootObject.[j].od_pair

Can someone kindly help me here please?


回答1:


You were right to start with the $. However, What you get with your particular JSON is List of HashMap<String, Object> where each JSON Object is represented as a single HashMap. Knowing that you can obtain the list of HashMaps like this:

List<HashMap<String, Object>> jsonObjectsInArray = path.getList("$");

The String will be the name of the attribute. The Object will be either String, Integer, JSONObject or JSONArray. The latter isn't exact class names but it's not relevant to you to achieve desired results.

Now, all we have to do is iterate over the HashMap and extract values of od_pair like this:

for (HashMap<String, Object> jsonObject : jsonObjectsInArray) {
    System.out.println(jsonObject.get("od_pair"));
}

The output is:

7015400:8727100
7015400:8814001

Hope it helps!



来源:https://stackoverflow.com/questions/57270072/getting-element-value-from-jsonpath-whose-root-is-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!