• 【python】json


    1. json

    啥是json,http://www.json.cn/wiki.html

    {
    "alarmRule": {
    "name": "al1",
    "comment": "description",
    "informType": 3,
    "timeValue": "1",
    "alarmLine": "90",
    "operator": null,
    "level": 3,
    "needtoUpdate":false,
    "snList": [{
    "sn": "123"
    },
    {
    "sn": "321"
    }],
    "interfaceList": []
    }
    }

    2. python json

    JSON类型   PYTHON类型
    对象 {} dict
    数组 [] list
    string   string,unicode
    123,123.45 int,float
    true/false True/False
    null None

    3. string of json 转换为 json in python

    反序列化,所有的字符串对象,默认都是Unicode,JSON标准中规定JSON编码就是unicode

    import json
    alarmvalue = """
            {
                "alarmRule": {
                    "name": "al1",
                    "comment": "description",
                    "informType": 3,
                    "timeValue": "1",
                    "alarmLine": "90",
                    "operator": null,
                    "level": 3,
                    "needtoUpdate":false,
                    "snList": [{
                        "sn": "123"
                    },
                    {
                        "sn": "321"
                    }],
                    "interfaceList": []
                }
            }
        """
    alarmvalueJson = json.loads(alarmvalue)
    print alarmvalueJson

    运行结果

    C:Python27python.exe C:/PycharmProjects/p3/src/pyproject1/iotest/jsonTest.py
    {u'alarmRule': {u'comment': u'description', u'name': u'al1', u'level': 3, u'timeValue': u'1', u'needtoUpdate': False, u'interfaceList': [], u'operator': None, u'alarmLine': u'90', u'snList': [{u'sn': u'123'}, {u'sn': u'321'}], u'informType': 3}}
    
    Process finished with exit code 0

    4. dict 转换为 string of json

    import json
    jsonOfDict = {
                'alarmRule': {
                    'comment': 'description',
                    'name': 'al1',
                    'level': 3,
                    'timeValue': '1',
                    'needtoUpdate': False,
                    'interfaceList': [],
                    'operator': None,
                    'alarmLine': '90',
                    'snList': [{
                        'sn': '123'
                    },
                    {
                        'sn': '321'
                    }],
                    'informType': 3
                }
            }
    jsonOfStr = json.dumps(jsonOfDict)
    print jsonOfStr

    执行脚本

    C:Python27python.exe C:/PycharmProjects/p3/src/pyproject1/iotest/jsonTest2.py
    {"alarmRule": {"comment": "description", "operator": null, "interfaceList": [], "name": "al1", "level": 3, "needtoUpdate": false, "timeValue": "1", "alarmLine": "90", "snList": [{"sn": "123"}, {"sn": "321"}], "informType": 3}}
    
    Process finished with exit code 0
  • 相关阅读:
    Python中匿名函数的应用
    Python中界面阻塞情况的解决方案
    Python中的协程,gevent模块
    Python中的进程和线程
    Python中的正则表达式用法
    Jquery瀑布流效果(下篇)
    安卓不支持keypress事件
    让MAC OS也能使用LL LA L等LS的别名
    git 常用命令
    javascript中的apply与call
  • 原文地址:https://www.cnblogs.com/AlexBai326/p/6836247.html
Copyright © 2020-2023  润新知