def find_node_item(json_file, search_key, match_result):
"""
节点查找,从hierarchy之后开始查找
:param json_file: json文件
:param search_key: 需要匹配的key
:param match_result: 需要匹配的value
:return:
"""
result = None
with open(json_file) as f:
json_s = json.loads(f.read())
def exec_node_search(json, search_key, match_result):
"""
执行节点查找
:param json: json
:param search_key: 需要匹配的key
:param match_result: 需要匹配的value
:return:
"""
try:
type_json = type(json)
print("do search type(json): %s" % (type_json))
if type(json) is dict:
node_ = json['node']
if node_ is None:
print("忽略")
elif isinstance(node_, dict):
if node_ is not None:
print("exec dict exec_node_search")
if utils.has_match_node(node_, search_key, match_result):
print("找到了需要的元素, 停止循环,停止dict递归")
return node_
else:
ret = exec_node_search(node_, search_key, match_result)
if ret is not None:
return ret
elif isinstance(node_, list):
print("处理列表数据")
for node_item in node_:
if node_item is not None:
print("exec list exec_node_search item: %s" % node_item)
res_id = node_item[search_key]
print("res_id: %s" % res_id)
if utils.has_match_node(node_item, search_key, match_result):
print("找到了需要的元素, 停止循环,停止list递归")
return node_item
else:
ret = exec_node_search(node_item, search_key, match_result)
print("ret = %s" % ret)
if ret is not None:
return ret
except Exception as e:
print("监听到了错误: %s" % (e.__str__()))
pass
result = exec_node_search(json_s['hierarchy'], search_key, match_result)
if result is not None:
print("find_node_item = %s" % result)
return result