JSON modules in Python

Source: Internet
Author: User

I. What is JSON

1. JSON is the abbreviation for JavaScript Object notation, which is a data interchange format

2. Essence is a string, is the string representation of the JS object

3. Similar to the dictionary in Python, but there are some grammatical differences

    • No value of None, only null value

    • Encoding format for Utf-8

    • The string must be double-quoted to express the

4. The string in Python is to be converted to a JSON object, using the JSON library to complete

two. Import the JSON module
Import JSON

Three. Common methods for JSON modules1. Json.dumps (str, indent=4, sort_keys=true) converts a Python data object into a JSON string
    • Indent: Format output JSON data, rendering more intuitive. Indent=4 indicates indentation of 4 spaces

    • Sort_keys:json strings are sorted by key in the dictionary

2. Json.loads () converts a JSON string into a Python object, such as a JSON string converted to a list, a dictionary

Four. ExampleExample 1
ImportJSON#convert a Python object to a JSON stringA = {"name":"Xiaozhai","Sex": None}b=Json.dumps (a)Print(b)Print(type (b))#convert a JSON string into a python dictionaryc =Json.loads (b)Print(c)Print(Type (c))

Run results

{"name":"Xiaozhai","Sex": null}<class 'Str'>{'name':'Xiaozhai','Sex': None}<class 'Dict'>

Note: The print () function saves quotes and prints in order to produce better-readable output, and if we do not print () in Python's own idle, we see better results

the role of indent in instance 2:json.dumps () is to indent spaces, Sorted_keys will be sorted by the first letter of key in the dictionary
Import JSON # converts a Python object to a JSON string a = {"name""Xiaozhai" "  sex"= Json.dumps (A, indent=4, sort_keys=True)print (b) Print (type (b))

Run results

{    "name""xiaozhai",    "  sex": null}<class'str' >

Five. Appendix1. Python Primitive type conversion table to JSON type
Python Json
Dict Object
List, tuple Array
STR, Unicode String
int, long, float Number
True True
False False
None Null

2. Conversion Chart of JSON type to Python type
Json Python
Object Dict
Array List
String Unicode
Number (int) int, long
Number (real) Float
True True
False False
Null None

Reference Articles

Http://www.runoob.com/python/python-json.html

Https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/ 001434499490767fe5a0e31e17e44b69dcd1196f7ec6fc6000

JSON modules 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.