0
我有一个json数据对象,并希望返回一个特定的值,如xpath XML。但在json上使用jsonpath。如何在python中使用jsonpath? (jsonpath_ng.ext)
该文档是好的,但我想念一个很好的例子。
我有一个json数据对象,并希望返回一个特定的值,如xpath XML。但在json上使用jsonpath。如何在python中使用jsonpath? (jsonpath_ng.ext)
该文档是好的,但我想念一个很好的例子。
因为我有大约1天的时间来了解它是如何工作的并错过了文档中的示例(https://pypi.python.org/pypi/jsonpath-ng/1.4.2),所以我在此处发布我的代码示例。
例如用于这样的结构:
"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 ""