簡體   English   中英

JSONPath:使用子值過濾器獲取根數組 object

[英]JSONPath: Get root array object using filter of child value

我試圖獲取 JSONPath 表達式來過濾我的 JSON 並使用子數組的值獲取整個運動 object。

我有以下 JSON:

[{

        "name": "Soccer",
        "regions": [{
                "name": "Australia",
                "leagues": [{
                        "name": "Australia league",
                        "inplay": 5,
                    }
                ]
            }
        ]
    }, {

        "name": "Tennis",
        "regions": [{
                "name": "Germany",
                "leagues": [{
                        "name": "Germany league",
                        "inplay": 0,
                    }
                ]
            }
        ]
    }
]

我需要使用 JsonPath 表達式獲取整個運動 object,其中“inplay == 0”。

結果應該是這樣的:

 {

    "name": "Tennis",
    "regions": [{
            "name": "Germany",
            "leagues": [{
                    "name": "Germany league",
                    "inplay": 0,
                }
            ]
        }
    ]
}

地區和聯賽計數可以 > 1 因此$[?(@.regions[0].leagues[0].inplay == 0)]不適合

試過$[?(@.regions[*].leagues[*].inplay == 0)]但它不起作用

這對我有用

$[?(@.regions[0].leagues[0].inplay == 0)]

由於 JayWay JSONPath 不直接支持(截至目前),我們利用contains作為解決方法

$[?(@.regions..inplay contains '0')]

注意:看起來 contains 的工作方式類似於“like”運算符或instr function,但此處並非如此。 如果inplay值包含 0,例如10 ,則不會提取記錄(根據我的測試;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM