首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JsonPath内部对象属性的差异

JsonPath内部对象属性的差异
EN

Stack Overflow用户
提问于 2017-06-08 13:23:28
回答 1查看 99关注 0票数 0

这是我的数据:

代码语言:javascript
复制
{
    "_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。

贾威JsonPath中尝试此操作将不起作用,并返回一个emtpy列表。要使它正常工作,我需要让这个查询$..[?(@.id)].images[2].link.href

为什么Jayway JsonPath中的第一个查询无效?

编辑:

这是我的测试方法

代码语言:javascript
复制
@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));
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-09 20:32:00

你在你的道路上错过了product

这会管用的-

代码语言:javascript
复制
List<String> href = cxt.read("$._embedded.product[?(@.id)].images[2].link.href");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44436988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档