Python json格式化打印实现过程解析


Posted in Python onJuly 21, 2020

编写python脚本,调试的时候需要打印json格式报文,直接打印看不出层次,可以使用json.dumps格式化打印

import json
import requests

def test_json():
  r=requests.get('https://home.testing-studio.com/categories.json')
  print(r.json())
  print(json.dumps(r.json(), indent=2,ensure_ascii=False)) # r.json()是json对象,indent表示缩进,ensure_ascii设置编码
格式化打印前:

格式化打印前:

Python json格式化打印实现过程解析

格式化打印后:

Python json格式化打印实现过程解析

json.dumps方法源码:

def 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 ``allow_nan`` is false, then it will be a ``ValueError`` to
  serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
  strict compliance of the JSON specification, instead of using the
  JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).

  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.

  If specified, ``separators`` should be an ``(item_separator, key_separator)``
  tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
  ``(',', ': ')`` otherwise. To get the most compact JSON representation,
  you should specify ``(',', ':')`` to eliminate whitespace.

  ``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.

  To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
  ``.default()`` method to serialize additional types), specify it with
  the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.

  """
  # cached encoder
  if (not skipkeys and ensure_ascii and
    check_circular and allow_nan and
    cls is None and indent is None and separators is None and
    default is None and not sort_keys and not kw):
    return _default_encoder.encode(obj)
  if cls is None:
    cls = JSONEncoder
  return cls(
    skipkeys=skipkeys, ensure_ascii=ensure_ascii,
    check_circular=check_circular, allow_nan=allow_nan, indent=indent,
    separators=separators, default=default, sort_keys=sort_keys,
    **kw).encode(obj)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python自动生产表情包
Mar 17 Python
基于Python3 逗号代码 和 字符图网格(详谈)
Jun 22 Python
Python实现输出程序执行进度百分比的方法
Sep 16 Python
python中学习K-Means和图片压缩
Nov 20 Python
教你用 Python 实现微信跳一跳(Mac+iOS版)
Jan 04 Python
代码分析Python地图坐标转换
Feb 08 Python
pandas创建新Dataframe并添加多行的实例
Apr 08 Python
Django认证系统实现的web页面实现代码
Aug 12 Python
Matplotlib使用Cursor实现UI定位的示例代码
Mar 12 Python
在python3.64中安装pyinstaller库的方法步骤
Jun 02 Python
python rsa-oaep加密的示例代码
Sep 23 Python
Python jieba库分词模式实例用法
Jan 13 Python
基于python实现删除指定文件类型
Jul 21 #Python
python打开音乐文件的实例方法
Jul 21 #Python
Python读取yaml文件的详细教程
Jul 21 #Python
Python中bisect的用法及示例详解
Jul 20 #Python
python为什么要安装到c盘
Jul 20 #Python
python如何代码集体右移
Jul 20 #Python
python接入支付宝的实例操作
Jul 20 #Python
You might like
Amazon Prime Video平台《无限住人 -IMMORTAL-》2020年开始TV放送!
2020/03/06 日漫
php面向对象全攻略 (十二) 抽象方法和抽象类
2009/09/30 PHP
PHP获取搜索引擎关键字来源的函数(支持百度和谷歌等搜索引擎)
2012/10/03 PHP
PHP中addcslashes与stripcslashes函数用法分析
2016/01/07 PHP
PHP-FPM运行状态的实时查看及监控详解
2016/11/18 PHP
PHP中Trait及其应用详解
2017/02/14 PHP
thinkphp5 migrate数据库迁移工具
2018/02/20 PHP
php设计模式之原型模式分析【星际争霸游戏案例】
2020/03/23 PHP
nodejs中实现路由功能
2014/12/29 NodeJs
jQuery动态背景图片效果实现方法
2015/07/03 Javascript
jQuery设置单选按钮radio选中/不可用的实例代码
2016/06/24 Javascript
jquery实现上传文件大小类型的验证例子(推荐)
2016/06/25 Javascript
js判断输入框不能为空格或null值的实现方法
2018/03/02 Javascript
Vue项目中配置pug解析支持
2019/05/10 Javascript
Vue CL3 配置路径别名详解
2019/05/30 Javascript
Vue路由的模块自动化与统一加载实现
2020/06/05 Javascript
JavaScript canvas实现跟随鼠标移动小球
2021/02/09 Javascript
Python中使用scapy模拟数据包实现arp攻击、dns放大攻击例子
2014/10/23 Python
python递归全排列实现方法
2018/08/18 Python
为什么str(float)在Python 3中比Python 2返回更多的数字
2018/10/16 Python
Mac下Anaconda的安装和使用教程
2018/11/29 Python
OpenCV+Python3.5 简易手势识别的实现
2020/12/21 Python
安德玛比利时官网:Under Armour比利时
2019/08/28 全球购物
服务员岗位责任制
2014/02/11 职场文书
2014厂务公开实施方案
2014/02/17 职场文书
慰问敬老院活动总结
2014/04/26 职场文书
关于运动会的口号
2014/06/07 职场文书
我心目中的好老师活动方案
2014/08/19 职场文书
党员志愿者活动方案
2014/08/28 职场文书
学生上课说话检讨书
2014/10/25 职场文书
业务员工作态度散漫检讨书
2014/11/02 职场文书
商务司机岗位职责
2015/04/10 职场文书
警示教育观后感
2015/06/17 职场文书
Golang二维切片初始化的实现
2021/04/08 Golang
Pytorch GPU内存占用很高,但是利用率很低如何解决
2021/06/01 Python
Django对接elasticsearch实现全文检索的示例代码
2021/08/02 Python