gpt4 book ai didi

java - Jsonpath 显示具有动态名称的子项

转载 作者:行者123 更新时间:2023-12-01 09:07:39 24 4
gpt4 key购买 nike

我有这个 json 数据,我要使用 jsonpath 解析它:

{
"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 端点的主机 header 而有所不同)。

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

/帕特里克

最佳答案

如果你只是想获取那些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**,那么解决方案应该是其他的。

关于java - Jsonpath 显示具有动态名称的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41137939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com