过滤列表中的JSON数据

时间:2016-06-03 01:18:41

标签: arrays json jsonpath

我是处理JSON数据的新手。我对如何返回颜色为“蓝色”的自行车有困难。使用JSONPath。

以下示例JSON文件。

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
        "bicycle": [
            {
                "price": 19.95
                "color": [
                    "red"
                ],
            },
            {
                "price": 20.99
                "color": [
                    "blue",
                    "Green"
                ],
            },
        ]
    }
}

我尝试使用以下过滤器,但它不起作用。

$.store.bicycle[?(@.color=='blue')]

任何想法如何让这个工作,只返回蓝色自行车的价格?

非常欢迎任何信息。

1 个答案:

答案 0 :(得分:0)

JSON数据

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            }],
        "bicycle": [
            {
                "price": 19.95,
                "color": [
                    "red"
                ]
            },
            {
                "price": 20.99,
                "color": [
                    "blue",
                    "Green"
                ]
            }
        ]
    }
}

JSONPath是

$.store.bicycle[?(@.color.indexOf('blue') != -1)]