Jsonpath用动态名称显示孩子

时间:2016-12-14 08:27:49

标签: java jsonpath

我有这个用jsonpath解析的json数据:

{
  "kind": "tm:sys:hardware:hardwarestats",
  "selfLink": "https://localhost/mgmt/tm/sys/hardware?ver\u003d11.5.4",
  "entries": {
    "https://localhost/mgmt/tm/sys/hardware/platform": {
      "nestedStats": {
        "entries": {
          "https://localhost/mgmt/tm/sys/hardware/platform/0": {
            "nestedStats": {
              "entries": {
                "baseMac": {
                  "description": "00:00ยง:00:00:00:00"
                },
                "biosRev": {
                  "description": "OBJ-0065-xx Build: 1.06.043.0 05/02/2014"
                },
                "marketingName": {
                  "description": "BIG-IP VPR-C2400"
                },
                "pvaVersion": {
                  "description": "20"
                }
              }
            }
          }
        }
      }
    }
  }
}

正如您所看到的,某些部分由根据此命名的儿童组成:

https://[host]/path

我希望能够通过使用通配符基本上忽略主机部分:

$.entries.https://*/mgmt/tm/sys/hardware/platform.nestedStats.entries.*.nestedStats.entries.marketingName.description

注意通配符替换localhost(它根据发送到api端点的主机头而不同)。

我无法控制服务器端。任何建议都表示赞赏!

/帕特里克

1 个答案:

答案 0 :(得分:1)

如果您只想获取那些没有过滤路径的baseMac,biosRev描述的值,这应该就够了

    public static void main(String[] args) {
    String samplejson = "{\n" +
            "  \"kind\": \"tm:sys:hardware:hardwarestats\",\n" +
            "  \"selfLink\": \"https://localhost/mgmt/tm/sys/hardware?ver\\u003d11.5.4\",\n" +
            "  \"entries\": {\n" +
            "    \"https://localhost/mgmt/tm/sys/hardware/platform\": {\n" +
            "      \"nestedStats\": {\n" +
            "        \"entries\": {\n" +
            "          \"https://localhost/mgmt/tm/sys/hardware/platform/0\": {\n" +
            "            \"nestedStats\": {\n" +
            "              \"entries\": {\n" +
            "                \"baseMac\": {\n" +
            "                  \"description\": \"00:00ยง:00:00:00:00\"\n" +
            "                },\n" +
            "                \"biosRev\": {\n" +
            "                  \"description\": \"OBJ-0065-xx Build: 1.06.043.0 05/02/2014\"\n" +
            "                },\n" +
            "                \"marketingName\": {\n" +
            "                  \"description\": \"BIG-IP VPR-C2400\"\n" +
            "                },\n" +
            "                \"pvaVersion\": {\n" +
            "                  \"description\": \"20\"\n" +
            "                }\n" +
            "              }\n" +
            "            }\n" +
            "          }\n" +
            "        }\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";

    Object baseMac = JsonPath.read(samplejson, "$.entries..nestedStats.entries.marketingName.description");
    System.out.println(baseMac.toString());
}

但是,如果你想阅读这些描述,只有某些路径,比如你只想阅读https://localhost/mgmt/tm/sys/hardware/platform/0而不是https://localhost/mgmt/tm/sys/hardware/platform/**1**,那么解决方案应该是别的。