2017-06-08 78 views
0

這是我的數據:差異JsonPath內對象屬性

{ 
    "_embedded": { 
     "analytics": { 
      "originCode": "PRD" 
     }, 
     "product": { 
      "id": "wi412784", 
      "description": "AH Wa­ter­fles met in­fu­ser blauw (500 ml)", 
      "unitSize": "per stuk", 
      "brandName": "AH", 
      "categoryName": "Koken, tafelen, non-food/Bidon", 
      "availability": { 
       "orderable": true, 
       "label": "Leverbaar" 
      }, 
      "priceLabel": { 
       "now": 3.49, 
       "was": 4.99 
      }, 
      "discount": { 
       "type": { 
        "name": "BONUS" 
       }, 
       "label": "30% korting" 
      }, 
      "images": [{ 
       "title": "Waterfles met infuser blauw (500 ml)", 
       "width": 80, 
       "height": 80, 
       "link": { 
        "href": "https://www.ah.nl.kpnis.nl/static/product/AHI_434d50323838313031_1_80x80_JPG.JPG" 
       } 
      }, { 
       "title": "Waterfles met infuser blauw (500 ml)", 
       "width": 200, 
       "height": 200, 
       "link": { 
        "href": "https://www.ah.nl.kpnis.nl/static/product/AHI_434d50323838313031_1_200x200_JPG.JPG" 
       } 
      }, { 
       "title": "Waterfles met infuser blauw (500 ml)", 
       "width": 708, 
       "height": 708, 
       "link": { 
        "href": "https://www.ah.nl.kpnis.nl/static/product/AHI_434d50323838313031_1_LowRes_JPG.JPG" 
       } 
      }, { 
       "title": "Waterfles met infuser blauw (500 ml)", 
       "width": 48, 
       "height": 48, 
       "link": { 
        "href": "https://www.ah.nl.kpnis.nl/static/product/AHI_434d50323838313031_1_48x48_GIF.GIF" 
       } 
      }] 
     } 
    } 
} 

產品裏面我想最後一個圖像的href字符串。

在Jsonpath評估網站,我知道: http://jsonpath.com/http://jsonpath.herokuapp.com/$._embedded.[?(@.id)].images[2].link.href是有效的JSON並返回HREF。

嘗試在Jayway JsonPath內不起作用並返回一個空列表。爲了得到它的工作,我需要做這個查詢$..[?(@.id)].images[2].link.href

爲什麼不是第一個查詢有效內Jayway JsonPath?

編輯:
這是我的測試方法

@Test 
    public void getImgSrc() { 
     String jsonData = "{\"_embedded\": {\"analytics\": {\"originCode\": \"PRD\"},\"product\": {\"id\": \"wi412784\",\"description\": \"AH Wa\u00ADter\u00ADfles met in\u00ADfu\u00ADser blauw (500 ml)\",\"unitSize\": \"per stuk\",\"brandName\": \"AH\",\"categoryName\": \"Koken, tafelen, non-food/Bidon\",\"availability\": {\"orderable\": true,\"label\": \"Leverbaar\"},\"priceLabel\": {\"now\": 3.49,\"was\": 4.99},\"discount\": {\"type\": {\"name\": \"BONUS\"},\"label\": \"30% korting\"},\"images\": [ {\"title\": \"Waterfles met infuser blauw (500 ml)\",\"width\": 80,\"height\": 80,\"link\": {\"href\": \"https://www.ah.nl.kpnis.nl/static/product/AHI_434d50323838313031_1_80x80_JPG.JPG\"}},{\"title\": \"Waterfles met infuser blauw (500 ml)\",\"width\": 200,\"height\": 200,\"link\": {\"href\": \"https://www.ah.nl.kpnis.nl/static/product/AHI_434d50323838313031_1_200x200_JPG.JPG\"}},{\"title\": \"Waterfles met infuser blauw (500 ml)\",\"width\": 708,\"height\": 708,\"link\": {\"href\": \"https://www.ah.nl.kpnis.nl/static/product/AHI_434d50323838313031_1_LowRes_JPG.JPG\"}},{\"title\": \"Waterfles met infuser blauw (500 ml)\",\"width\": 48,\"height\": 48,\"link\": {\"href\": \"https://www.ah.nl.kpnis.nl/static/product/AHI_434d50323838313031_1_48x48_GIF.GIF\"}}]}}}"; 

     DocumentContext cxt = JsonPath.parse(jsonData); 
     List<String> href = cxt.read("$._embedded.[?(@.id)].images[2].link.href"); 
     Assert.assertEquals("https://www.ah.nl.kpnis.nl/static/product/AHI_434d50323838313031_1_LowRes_JPG.JPG", href.get(0)); 
    } 

回答

0

你缺少在你的路徑product

這將工作 -

List<String> href = cxt.read("$._embedded.product[?(@.id)].images[2].link.href"); 
+0

是的,有明顯的Jayway JsonPath和Goessner JsonPath之間的細微差別。 –