Python 判断 JSON 数据中是否存在 'error': true
可以使用 Python 内置的 json 库来解析 JSON 字符串,然后查询是否存在 'error': true。
代码示例:
import json
data = '{"error":true,"errmsg":"[820013] 请按顺序进行翻页查询"}'
json_data = json.loads(data)
if 'error' in json_data and json_data['error']:
print('存在error为true')
else:
print('不存在error为true')
输出:
存在error为true
解释:
首先使用 json.loads 将 JSON 字符串解析为 Python 字典对象,然后判断字典中是否存在 'error' 键,并且其对应的值为 True。如果存在,则说明查询存在 'error': true。否则,说明不存在。
原文地址: https://www.cveoy.top/t/topic/olVe 著作权归作者所有。请勿转载和采集!