JSON in Python

Source: Internet
Author: User

#-*-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

Related Article

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.