In Python: between dict (or object) with each conversion json

In Python language, and data conversion between json dict dictionary and the object, the operation is essential.

It comes json library in Python. Introduced through import json.

There are two methods in the json module,

  • loads (): json data into the data dict
  • dumps (): dict data into the data json
  • load (): read json file data converted into data dict
  • Dict json file write data into the data json: dump ()

The following are specific examples:

turn json data dictionary dict

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

Json data transfer objects

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import json
def obj_to_json():
    stu = Student('007', '007', 28, 'male', '13000000000', '[email protected]')
    print(type(stu))  # <class 'json_test.student.Student'>
    stu = stu.__dict__  # 将对象转成dict字典
    print(type(stu))  # <class 'dict'>
    print(stu)  # {'id': '007', 'name': '007', 'age': 28, 'sex': 'male', 'phone': '13000000000', 'email': '[email protected]'}
    j = json.dumps(obj=stu)
    print(j)  # {"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "[email protected]"}
if __name__ == '__main__':
    obj_to_json()

dict dictionary data into json

import json

def json_to_dict():
    j = '{"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "[email protected]"}'
    dict = json.loads(s=j)
    print(dict)  # {'id': '007', 'name': '007', 'age': 28, 'sex': 'male', 'phone': '13000000000', 'email': '[email protected]'}

if __name__ == '__main__':
    json_to_dict()

json data into objects

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import json
def json_to_obj():
    j = '{"id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "[email protected]"}'
    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: [email protected]
if __name__ == '__main__':
    json_to_obj()

Using the json load () and dump () method

  • Use dump () method
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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)  # 会在目录下生成一个1.json的文件,文件内容是dict数据转成的json数据

if __name__ == '__main__':
    dict_to_json_write_file()
  • load () use
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()

Guess you like

Origin blog.51cto.com/14246112/2458181
Recommended