欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python之json模块

程序员文章站 2024-02-03 10:05:34
...

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:\python36\python3.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:\python36\python3.exe E:/uiautomator/test.py
{"CityId": 18, "CityName": "西安", "ProvinceId": 27, "CityOrder": 1, "c": false}
<class 'str'>

  

 

上一篇: 2.js和go函数对比

下一篇: