2017-06-08 1 views
0

Voici mes données:différence dans la propriété d'objet interne 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" 
       } 
      }] 
     } 
    } 
} 

intérieur produit que je veux obtenir la chaîne href de la dernière image.

Sur les sites évaluateur Jsonpath que je connais: http://jsonpath.com/ et http://jsonpath.herokuapp.com/ ce $._embedded.[?(@.id)].images[2].link.href est valide JSON et renvoie le href.

Si vous essayez cela à l'intérieur Jayway JsonPath ne fonctionnera pas et renvoie une liste emtpy. Pour que cela fonctionne, j'ai besoin de faire cette requête $..[?(@.id)].images[2].link.href

Pourquoi la première requête n'est-elle pas valide dans Jayway JsonPath?

Edit:
Ceci est ma méthode d'essai

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

Répondre

0

Il vous manque product dans votre chemin.

Cela fonctionne -

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

Oui, il y a apparemment des différences mineures entre Jayway JsonPath et Goessner JsonPath. –