Getting element value from jsonpath whose root is an array

岁酱吖の 提交于 2019-12-02 04:38:18

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!

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