8.20. json 模块 API 文档

8.20.1. API

def _cjson_encode(cjson:cjson.cJSON):...
def loads(json:str)->dict:...
def _cjson_decode(d:dict):...
def dumps(d:dict)->str:...

8.20.2. Examples

8.20.2.1. json_dumps.py

import json

s0 = json.dumps(1)

s1 = json.dumps({"a": 1, "b": 2, "c": 3})
print(s1)

s2 = json.dumps(
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": {
            "e": 4,
            "f": 5
        }
    }
)
print(s2)

s3 = json.dumps(
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": {
            "e": 4,
            "f": 5
        },
        "g": [
            6,
            7,
            8
        ]
    }
)
print(s3)

s4 = json.dumps(
    {
        "a": 1,
        "b": 2,
        "c": 3,
        "d": {
            "e": 4,
            "f": 5
        },
        "g": [
            6,
            7,
            8
        ],
        "h": None,
        "i": False,
        "j": True,
        "k": "string",
        "l": 1.234
    }
)
print(s4)

8.20.2.2. json_loads.py

import json

json.loads('{"a": 1, "b": 2, "c": 3}')

json.loads('{"a": 1, "b": 2, "c": 3, "d": {"e": 4, "f": 5}}')

json.loads('{"a": 1, "b": 2, "c": 3, "d": {"e": 4, "f": 5}, "g": [6, 7, 8]}')

json.loads('{"a": 1, "b": 2, "c": 3, "d": {"e": 4, "f": 5}, "g": [6, 7, 8], "h": null}')

json.loads('{"a": 1, "b": 2, "c": 3, "d": {"e": 4, "f": 5}, "g": [6, 7, 8], "h": null, "i": false}')

json.loads('{"a": 1, "b": 2, "c": 3, "d": {"e": 4, "f": 5}, "g": [6, 7, 8], "h": null, "i": false, "j": true}')

json.loads('{"a": 1, "b": 2, "c": 3, "d": {"e": 4, "f": 5}, "g": [6, 7, 8], "h": null, "i": false, "j": true, "k": "string"}')

json.loads('{"a": 1, "b": 2, "c": 3, "d": {"e": 4, "f": 5}, "g": [6, 7, 8], "h": null, "i": false, "j": true, "k": "string", "l": 1.234}')