json字符串对象转字典,采用loads方法,当转换后的字典有Boolean类型的时候,要注意json字符串的值小写:
import json a='{"CityId":18,"CityName":"西安","ProvinceId":27,"CityOrder":1,"c":null,"d":true,"e":false}' b=json.loads(a) print (b) print (type(b)) print (b["d"]) print (type(b["d"])) E:python36python3.exe E:/uiautomator/test.py {'CityId': 18, 'CityName': '西安', 'ProvinceId': 27, 'CityOrder': 1, 'c': None, 'd': True, 'e': False} <class 'dict'> True <class 'bool'>
字典转json字符串,采用dumps方法,如果转换的字典带有中文需要带参数ensure_ascii = False,确保原样输出:
import json a={"CityId":18,"CityName":"西安","ProvinceId":27,"CityOrder":1,"c":False} b=json.dumps(a,ensure_ascii = False) print (b) print (type(b)) E:python36python3.exe E:/uiautomator/test.py {"CityId": 18, "CityName": "西安", "ProvinceId": 27, "CityOrder": 1, "c": false} <class 'str'>