使用jsonpath获取匹配元素的父级 [英] Getting parent of matched element with jsonpath

查看:899
本文介绍了使用jsonpath获取匹配元素的父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个JSON对象,例如:

Let's say I have a JSON object such as this:

[{
    name: "A",
    sub: [
        {prop: "1"},
        {prop: "2"},
        {prop: "3"},
    ]
},
{
    name: "B",
    sub: [
        {prop: "7"},
        {prop: "8"},
        {prop: "9"},
    ]    
}];

如何获得 prop value是 2

How can I get the parent of the element who's prop value is 2?

jsonpath.query(data, "$..[?(@.prop==2)]");

这将返回对象本身,但我不知道如何获取它的父级。

This will return the object itself, but I don't know how to get it's parent.

推荐答案

我最终使用 parent 而不是查询方法。就如此容易。当我阅读文档时,我只是错过了它。

I ended up using parent instead of query method. As simple as that. I just missed it when I was reading the docs.

这篇关于使用jsonpath获取匹配元素的父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆