MongoDB ObjectId類型 序列化問題


MongoDB ObjectId類型 序列化問題

data = mongo的Bson數據

import json
from bson import json_util
data = json.loads(
    json.dumps(
        data,
        sort_keys=False,
        indent=4,
        default=json_util.default)
    )

JSON (JavaScript Object Notation)

http://json.org is a subset of
JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data
interchange format.

Pretty printing::

    >>> import json
    >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
    {
        "4": 5,
        "6": 7
    }

json.dumps()

If ``indent`` is a non-negative integer, then JSON array elements and
object members will be pretty-printed with that indent level. An indent
level of 0 will only insert newlines. ``None`` is the most compact
representation.

``default(obj)`` is a function that should return a serializable version
of obj or raise TypeError. The default simply raises TypeError.

If *sort_keys* is true (default: ``False``), then the output of
dictionaries will be sorted by key.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM