2017-07-28 90 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 ""