An instance of Python dict of or an instance of an object being converted to or from json

  • 2020-11-03 22:31:15
  • OfStack

In Python, conversion of json data to dict dictionaries and objects is essential.

Come with the json library in Python. Import through import json.

There are two methods in json module,

loads() : Converts json data to dict data

dumps() : Convert dict data to json data

load() : Reads the json file data and converts it to dict data

dump() : Converts dict data to json data and writes to json file

Here are some concrete examples:

dict dictionary transfers json data


import json
def dict_to_json():
 dict = {}
 dict['name'] = 'many'
 dict['age'] = 10
 dict['sex'] = 'male'
 print(dict) #  Output: {'name': 'many', 'age': 10, 'sex': 'male'}
 j = json.dumps(dict)
 print(j) #  Output: {"name": "many", "age": 10, "sex": "male"}
if __name__ == '__main__':
 dict_to_json()

Object to json data


import json
def obj_to_json():
 stu = Student('007', '007', 28, 'male', '13000000000', '123@qq.com')
 print(type(stu)) # <class 'json_test.student.Student'>
 stu = stu.__dict__ #  Converts the object to dict The dictionary 
 print(type(stu)) # <class 'dict'>
 print(stu) # {'id': '007', 'name': '007', 'age': 28, 'sex': 'male', 'phone': '13000000000', 'email': '123@qq.com'}
 j = json.dumps(obj=stu)
 print(j) # {"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "123@qq.com"}
if __name__ == '__main__':
 obj_to_json()

json data converted to dict dictionary


import json
def json_to_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'}
if __name__ == '__main__':
 json_to_dict()

json data turned into objects


import json
def json_to_obj():
 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
if __name__ == '__main__':
 json_to_obj()

Use of load() and dump() methods of json

The use of dump() method


import json
def dict_to_json_write_file():
 dict = {}
 dict['name'] = 'many'
 dict['age'] = 10
 dict['sex'] = 'male'
 print(dict) # {'name': 'many', 'age': 10, 'sex': 'male'}
 with open('1.json', 'w') as f:
 json.dump(dict, f) #  Will be generated in the directory 1 a 1.json The contents of the file are dict Of the data json data 
if __name__ == '__main__':
 dict_to_json_write_file()

The use of load ()


import json
def json_file_to_dict():
 with open('1.json', 'r') as f:
 dict = json.load(fp=f)
 print(dict) # {'name': 'many', 'age': 10, 'sex': 'male'}
if __name__ == '__main__':
 json_file_to_dict()

Related articles: