2016-12-14 50 views
0

我有這樣的JSON數據,我是用jsonpath解析: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端點而不同)。

我無法控制服務器端。任何建議表示讚賞!

/帕特里克

+0

請不要把你的問題的重要部分放在像pastebin這樣的外部系統上。還請將您的問題(包括JSON)的大小縮減爲說明問題所需的內容。見http://www.sscce.org/ – 2016-12-14 08:32:03

回答

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

但是,如果你想讀這些描述WRT只某些路徑,如您想要只讀https://localhost/mgmt/tm/sys/hardware/platform/0而不是https://localhost/mgmt/tm/sys/hardware/platform/**1**,那麼解決方案應該是別的。

+0

謝謝!可悲的是,我所追求的是第二種方式。我已經做了一些改變(實際上或多或少與你的回答相同),我認爲沒有辦法像我最初想要的那樣去做。 – PatrikJ