如何将json数据转换成python内部的数据类型
展示一下zabbix的接口返回的json数据(数据经过dumps编码了,因为原数据为str类型,只有一行,不易读)
js = json.dumps(get_alert(), indent=4, ensure_ascii=False)
print(js)
# get_alert()方法为获取json数据,编码后赋给js,打印js,结果如下:
# indent = 4意为设置缩进为4个空格,
# ensure_ascii=False参数跟单网是禁用ascii编码,若不禁用,中文字符会输出为ASCII码
{
"jsonrpc": "2.0",
"result": [
{
"triggerid": "123456",
"expression": "{23567}>95",
"description": "High memory utilization > 95",
"url": "",
"status": "0",
"value": "1",
"priority": "4",
"lastchange": "123456",
"comments": "",
"error": "",
"templateid": "0",
"type": "0",
"state": "0",
"flags": "0",
"recovery_mode": "0",
"recovery_expression": "",
"correlation_mode": "0",
"correlation_tag": "",
"manual_close": "0",
"opdata": "",
"hosts": [
{
"hostid": "8888",
"name": "window_sever"
}
],
"items": [
{
"itemid": "123456",
"name": "Memory utilization",
"description": "Memory used percentage is calculated as (100-pavailable)"
}
]
},
{
"triggerid": "17099",
"expression": "{20221}<{$SWAP.PFREE.MIN.WARN} and {20222}>0",
"description": "High swap space usage ( less than 20% free)",
"url": "",
"status": "0",
"value": "1",
"priority": "2",
"lastchange": "123456789",
"comments": "This trigger is ignored, if there is no swap configured",
"error": "",
"templateid": "16176",
"type": "0",
"state": "0",
"flags": "0",
"recovery_mode": "0",
"recovery_expression": "",
"correlation_mode": "0",
"correlation_tag": "",
"manual_close": "0",
"opdata": "Free: {ITEM.LASTVALUE1}, total: {ITEM.LASTVALUE2}",
"hosts": [
{
"hostid": "10325",
"name": "linus"
}
],
"items": [
{
"itemid": "31681",
"name": "Free swap space in %",
"description": ""
},
{
"itemid": "123456",
"name": "Total swap space",
"description": ""
}
]
}
],
"id": "3"
}
接下来我们需要对json对象进行解码
js_loads_data = json.loads(js)
# 解码后的数据转为python原生的字典类型(dict)