如何在 python 中使用 jsonpath? (jsonpath_ng.ext)

how can I use jsonpath in python? (jsonpath_ng.ext)

我有一个 json 数据对象,并且想要 return 一个特定的值,例如 XML 中的 xpath。但是在 json 上 jsonpath.

文档没问题,但我错过了一个很好的例子。

因为我有大约 1 天的时间来了解它是如何工作的并且错过了文档中的示例 (https://pypi.python.org/pypi/jsonpath-ng/1.4.2),所以我 post 我的代码示例在这里。

这样的结构示例:

"abilities": [
            {
          ...
                "name": "device_info",
                "properties": [
                    {
                        "name": "manufacturer",
                        "value": "xxxx",
                    },
                    {
                        "name": "product",
                        "value": "yyy",

                    }
                ],
                "type": "device_info"
            },
            {....}
            ]

获得能力值的代码属性:

from jsonpath_ng.ext import parse

abilityname = "device_info"
propertyname = "manufacturer"
result = parse('$[?(@.name=="' + abilityname + '")].properties[?(@.name=="' + propertyname + '")]').find(myJson)
if len(result) == 1:
    return str(result[0].value['value'])
else:
    return ""