1、字典 dict转 json :
dict = {'q':'22'}
json.dumps(dict)
输出为 {"q":"22"} 单引号变成双引号
2、将对象转成字典dict
stu = Student('007', '007', 28, 'male', '13000000000', '123@qq.com')
print(type(stu)) # <class 'json_test.student.Student'>
stu = stu.dict # 将对象转成dict字典
print(type(stu))
3、json数据转成dict字典
j = '{"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "123@qq.com"}'
dict = json.loads(s=j)
print(dict) # {'id': '007', 'name': '007', 'age': 28, 'sex': 'male', 'phone': '13000000000', 'email': '123@qq.com'}
双引号变成单引号
4、json数据转成对象
'''
j = '{"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "123@qq.com"}'
dict = json.loads(s=j)
stu = Student()
stu.dict = dict
print('id: ' + stu.id + ' name: ' + stu.name + ' age: ' + str(stu.age) + ' sex: ' + str(
stu.sex) + ' phone: ' + stu.phone + ' email: ' + stu.email) # id: 007 name: 007 age: 28 sex: male phone: 13000000000 email: 123@qq.com
'''
网友评论