#-*-coding:utf-8-*-#Author:keekuun"""JSON: Transitive objects between different languages JSON Python data type correspondence {} dict[] list "string" str123.6 Int/floattrue/false true/falsenull None"""ImportJSON"""Note the py file name is not json.py named, otherwise the return error: module ' JSON ' has no attribute ' dumps '"""#the dict in PythonD = {'name':'Bob',' Age': 20}#The dumps () method returns a str with the content being the standard JSON. Similarly, the dump () method can write JSON directly to a file-like Object. Dict2json =Json.dumps (d)Print(Dict2json)#{"name": "Bob", "Age":Print(Type (Dict2json))#<class ' str ' >#Deserializes json into a Python object, using loads () or the corresponding load () method, loads () deserializes the JSON string, and load () reads the string from the File-like object and deserializes itJson2dict =json.loads (Dict2json)Print(json2dict)#{' name ': ' Bob ', ' age ':#JSON advanced usage#changes the student instance to a JSON {} objectclassStudent:def __init__(self, name, age, score): Self.name=name Self.age=Age Self.score=scorest= Student ('Bob', 20, 100)#the first method, the custom functiondefst2dict (STD): D= {'name': Std.name,' Age': Std.age,'score': Std.score}returnD
JSON in Python