从根目录为数组的jsonpath获取元素值 [英] Getting element value from jsonpath whose root is an array

查看:1685
本文介绍了从根目录为数组的jsonpath获取元素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON响应,该响应的根是1个或多个对象的数组.我想提取每个对象中元素之一的值.

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.

这是JSON示例:

[  
   {  
      "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
         }
      ]
   }
]

我想访问每个对象内的od_pair的值.

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.

这是我编写的代码段:

        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);
        }

这是我看到的错误:

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?

推荐答案

您从$开始是正确的.但是,使用特定JSON得到的是HashMap<String, Object>中的List,其中每个JSON对象都表示为单个HashMap.知道您可以像这样获取HashMap的列表:

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("$");

String将是属性的名称. Object将是StringIntegerJSONObjectJSONArray.后者不是确切的类名,但与您获得期望的结果无关.

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.

现在,我们要做的就是遍历HashMap并提取od_pair的值,如下所示:

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"));
}

输出为:

7015400:8727100
7015400:8814001

希望有帮助!

这篇关于从根目录为数组的jsonpath获取元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆