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实现的彩票机选器实例
Jun 17 Python
Python的Django框架中forms表单类的使用方法详解
Jun 21 Python
Python中if elif else及缩进的使用简述
May 31 Python
python实现pdf转换成word/txt纯文本文件
Jun 07 Python
Django 视图层(view)的使用
Nov 09 Python
关于python中密码加盐的学习体会小结
Jul 15 Python
django-rest-framework解析请求参数过程详解
Jul 18 Python
Python学习笔记之列表和成员运算符及列表相关方法详解
Aug 22 Python
使用OpenCV实现仿射变换—平移功能
Aug 29 Python
MATLAB数学建模之画图汇总
Jul 16 Python
详解python with 上下文管理器
Sep 02 Python
详解使用python爬取抖音app视频(appium可以操控手机)
Jan 26 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
非常好的php目录导航文件代码
2006/10/09 PHP
利用php+mcDropdown实现文件路径可在下拉框选择
2013/08/07 PHP
ThinkPHP3.2.2的插件控制器功能
2015/03/05 PHP
laravel5.6中的外键约束示例
2019/10/23 PHP
PHP如何获取Cookie并实现模拟登录
2020/07/16 PHP
Javascript中Eval函数的使用
2010/03/23 Javascript
页面元素绑定jquery toggle后元素隐藏的解决方法
2014/03/27 Javascript
javascript中的正则表达式使用指南
2015/03/01 Javascript
jQuery实现复选框批量选择与反选的方法
2015/06/17 Javascript
JS实现网页上随机产生超链接地址的方法
2015/11/09 Javascript
全面解析DOM操作和jQuery实现选项移动操作代码分享
2016/06/07 Javascript
基于jQuery实现的打字机效果
2017/01/16 Javascript
原生js实现弹出层效果
2017/01/20 Javascript
js基于myFocus实现轮播图效果
2017/02/14 Javascript
vue路由懒加载的实现方法
2018/03/12 Javascript
vue 点击按钮实现动态挂载子组件的方法
2018/09/07 Javascript
vue2 设置router-view默认路径的实例
2018/09/20 Javascript
JS几个常用的函数和对象定义与用法示例
2020/01/15 Javascript
[10:34]DOTA2上海特级锦标赛全纪录
2016/03/25 DOTA
Python文件夹与文件的操作实现代码
2014/07/13 Python
在Docker上部署Python的Flask框架的教程
2015/04/08 Python
Python基础学习之常见的内建函数整理
2017/09/06 Python
基于Django URL传参 FORM表单传数据 get post的用法实例
2018/05/28 Python
Python面向对象之接口、抽象类与多态详解
2018/08/27 Python
python3 xpath和requests应用详解
2020/03/06 Python
英国男士时尚网站:Dandy Fellow
2018/02/09 全球购物
美国瑜伽服装和装备购物网站:Mukha Yoga
2019/02/22 全球购物
世界各地的旅游、观光和活动:Isango!
2019/10/29 全球购物
飞利浦美国官网:Philips美国
2020/02/28 全球购物
物业管理毕业生个人的求职信
2013/11/30 职场文书
2014年教师节国旗下讲话稿
2014/09/10 职场文书
交通事故赔偿协议书
2014/10/16 职场文书
业务内勤岗位职责
2015/04/13 职场文书
MySQL基础(一)
2021/04/05 MySQL
如何自己动手写SQL执行引擎
2021/06/02 MySQL
React-vscode使用jsx语法的问题及解决方法
2021/06/21 Javascript