JsonPath内部对象属性的差异

时间:2017-06-08 13:23:29

标签: java json 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内尝试此操作不会起作用并返回一个emtpy List。为了使其正常工作,我需要进行此查询$..[?(@.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));
    }

1 个答案:

答案 0 :(得分:0)

您的路径中缺少product

这将有效 -

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