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