python json函数(python基础学习)

本文介绍了Python的json模块,重点讲解了json.dumps()和json.loads()两个关键函数的用法。dumps()用于将Python对象转换为JSON字符串,支持sort_keys、indent和ensure_ascii等参数,以便于数据格式化和中文显示。而loads()则用于将JSON字符串转换回Python对象。在处理中文字符时,需设置dumps()的ensure_ascii参数为False。
摘要由CSDN通过智能技术生成

使用之前需要导入json函数

import json
json.dumps() 将python对象编码成json字符串
json.loads() 将已编码的json字符串解码成python对象

json.dumps()

查看dumps的用法

>>> import json
>>> help(json.dumps)
Help on function dumps in module json:

dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
    Serialize ``obj`` to a JSON formatted ``str``.

    If ``skipkeys`` is true then ``dict`` keys that are not basic types
    (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
    instead of raising a ``TypeError``.

    If ``ensure_ascii`` is false, then the return value can contain non-ASCII
    characters if they appear in strings contained in ``obj``. Otherwise, all
    such characters are escaped in JSON strings.

    If ``check_circular`` is false, then the circular reference check
    for container types will be skipped and a circular reference will
    result in an ``OverflowError`` (or worse).

    If `
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值