def formatjson(response, searchkey):
for key, value in response.items():
if key == searchkey:
return value
# 如果value为字典
elif isinstance(value, dict):
if searchkey in str(value):
data = formatjson(value, searchkey)
if data:
return data
# value为列表
elif isinstance(value, list):
if searchkey in str(value):
for sub_value in value:
data = formatjson(sub_value, searchkey)
if data:
return data
data = {'code': 0, 'message': {'EN': 'ok', 'CN': 'ok'}, 'content': {'products': [{'id': 42, 'productId': 80040}]}}
print(formatjson(data,'productId'))
执行结果为:80040