2017-07-28 47 views

回答

1

因为我有大约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 ""