Python21天打卡Day11-dict和json格式互转

Python21天打卡Day11-dict和json格式互转_第1张图片

import json
a={'name':'idoxu','sex':'male','age':30}
b=json.dumps(a)#把字典转为json
print('{},{}'.format(b,type(b)))
#json的格式是字符串

c='{"name":"idoxu","sex":"male","age":30}'
d=json.loads(c)#json转为dict
#the JSON object must be str, bytes or bytearray, not dict
print('{},{}'.format(d,type(d)))

e={"name":"idoxu","sex":"male","age":30}
print('{},{}'.format(e,type(e)))

{“name”: “idoxu”, “sex”: “male”, “age”: 30},
{‘name’: ‘idoxu’, ‘sex’: ‘male’, ‘age’: 30},
{‘name’: ‘idoxu’, ‘sex’: ‘male’, ‘age’: 30},

你可能感兴趣的:(Python)