Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3657099
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2021-01-28 17:04:22

如何将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

 

阅读(20722) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~