Conversion between JSON and dict in Python

Source: Internet
Author: User

The Dict (or object) of Python and the conversion of JSON to each other

In the Python language, the conversion between JSON data and dict dictionaries and objects is an essential operation.

Bring your own JSON library in Python. by import json Importing.

There are 2 methods in the JSON module,

    • loads(): Convert JSON data to dict data
    • dumps(): Convert dict data to JSON data
    • load(): Reads JSON file data and turns it into dict data
    • dump(): The JSON file is written after converting dict data into JSON data

Here's a concrete example:

Dict Dictionary to JSON data
import Jsondef dict_to_json (): Dict = {} dict[ ' name '] =  ' many ' Dict[ ' age '] = 10 dict[ ' sex '] =  Male ' Print (dict) # output: {' name ': ' Many ', ' age ': ten, ' sex ': ' Male ' } j = Json.dumps (dict) print (j) # output: {"name": "Many", "age": Ten, "Sex": "Male"}if __name__ = =  ' __main__ ': Dict_to_json ()      
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
Object Goto JSON data
Import Jsondef Obj_to_json (): Stu = Student (' 007 ',' 007 ',28,' Male ',' 13000000000 ',' [email protected] ')PrintType (stu)) # <class' Json_test.student.Student ' > stu = stu.__dict__ # Convert object to dict dictionaryPrintType (stu)) # <class' Dict ' >Print (stu) # {' ID ':' 007 ',' Name ':' 007 ',' Age ': ' sex ':  ' male ',  ' phone ':  ' 13000000000 ',  ' email ': " [email protected] '} j = Json.dumps (obj=stu) print (j) # {" id ": " 007 ", " name ": " age ": " sex ": " male ", " phone ": " 13000000000 ", " email ": " [email protected] "}if __name__ = =  ' __main__ ': Obj_to_json ()         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
JSON data into Dict dictionary
import jsondef 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()
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
Convert JSON data to objects
import jsondef json_to_obj (): j =  ' ID: ' + stu.id +  ' name: ' + stu.name + 
           
             ' Age: ' + str (stu.age) + 
             ' sex: ' + str (stu.sex) + if __name__ =  ' __main__ ': json_to_obj ()    
                 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
JSON's load()And dump()Use of methods
    • dump()Use of methods
import jsondef dict_to_json_write_file (): Dict = {} Dict[ ' name '] =  ' many ' Dict[ ' age '] = Span class= "Hljs-number" >10 dict[ ' sex '] =  ' male ' print (dict) Span class= "Hljs-comment" ># {' name ': ' Many ', ' age ': ten, ' sex ': ' Male '} with open ( ' 1.json ',  ' W ') as f:json.dump (Dict, F) # will generate a 1.json file in the directory, the file content is dict data into the JSON data if __name__ =  ' __main__ ': Dict_to_json_write_file ()         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • load()The use
import jsondef 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()

Conversion between JSON and dict in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.