Python3 's JSON module uses

Source: Internet
Author: User

1. Introduction to the JSON module

JSON is a module of Python's own operation JSON.

Data type conversion Relationships when Python is serialized as JSON:

Python format JSON format
Dict (composite type) Object
list, tuple (collection type) Array
int, long, float (numeric type) Number
STR, Unicode String
True True
False False
None Null

JSON deserializes to Python data type control relationship:

JSON format Pyth On format
object Dict
array list
string Unicode
number (int) int, long
Numer (Real) float
True True
false
null None

2. How to use

The JSON library provides several APIs:

Json.dumps (): Serializing a dictionary to a JSON string

Json.loads (): Deserializing a JSON string into a dictionary

Json.dump (): Serializes a dictionary to a file, is a text file, or is equivalent to writing a serialized JSON string to a file

Json.load (): Deserializing a dictionary from a file

Summary: No S is serialized to a file or deserialized from a file, with S is both in-memory operation does not involve persistence

A simple use example is as follows:

#! /usr/bin/pythonimport jsonif __name__ = = ' __main__ ':    cc = {        "name": "CC11001100",        "age": $,        "money": 9.9,        "car": "Feng-huang Bicycle",        "house": "zu Zhai",        "girl friend": None,        "hobby": "Thinking ..."    }    # Serialized As String    json_string = Json.dumps (cc)    print (json_string)    # Deserializes from string    json_dict = Json.loads (json_string)    print (json_dict)    # Serialized to file with    open (' D:/cc.json ', ' W ') as Json_file:        Json.dump (CC, Json_file)    # Deserialize with    open (' D:/cc.json ', ' R ') as Json_file from the file:        json_dict = Json.load ( Json_file)        print (json_dict)

Several parameters that will be accepted when the Py object is serialized as JSON:

Indent: That is, the indent is a few spaces, when you need to format the output is generally set to 4 spaces

A small example of specifying indent:

#! /usr/bin/pythonimport jsonif __name__ = = ' __main__ ':    cc = {        "name": "CC11001100",        "age": $,        "money": 9.9,        "car": "Feng-huang Bicycle",        "house": "zu Zhai",        "girl friend": None,        "hobby": "Thinking ..."    }    Print (Json.dumps (cc, indent=4))

Output:

{    "name": "CC11001100",    "Age": "$    ": 9.9, "    car": "Feng-huang Bicycle", "House    ": "\ U7956\u5b85 ",    " girl friend ": null,    " hobby ":" Thinking ... "}

Separators: The delimiter used to generate the JSON substring is used instead of separating multiple k/v pairs, and separating the k/v:

A small example that specifies a separators:

#! /usr/bin/pythonimport jsonif __name__ = = ' __main__ ':    cc = {        "name": "CC11001100",        "age": $,        "money": 9.9,        "car": "Feng-huang Bicycle",        "house": "zu Zhai",        "girl friend": None,        "hobby": "Thinking ..."    }    Print (Json.dumps (CC, indent=4, separators= (' ↓ ', ' → ')))

Output:

{    ' name ' → ' CC11001100 ' ↓    ' age ' →22↓    ' money ' →9.9↓    "car" → "Feng-huang Bicycle" ↓    "house" → "\ u7956\u5b85 "↓    " girl friend "→null↓    " hobby "→" Thinking ... "}

Sort_keys: Whether to sort key or not, I don't know what to do with it yet.

3. Limitations of the self-brought JSON module

It is convenient to use the JSON module that comes with it, but when serializing a custom type, it throws a TypeError exception:

Typeerror:object of type ' person ' was not JSON serializable

For the serialization of custom types, a third-party library is generally used, but this is something else.

Resources:

1. https://docs.python.org/2/library/json.html

Python3 's JSON module uses

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.