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内置函数的用法实例教程
Sep 08 Python
Python简单实现enum功能的方法
Apr 25 Python
python类:class创建、数据方法属性及访问控制详解
Jul 25 Python
python学习教程之使用py2exe打包
Sep 24 Python
python数据抓取分析的示例代码(python + mongodb)
Dec 25 Python
实用自动化运维Python脚本分享
Jun 04 Python
python批量下载抖音视频
Jun 17 Python
python通过robert、sobel、Laplace算子实现图像边缘提取详解
Aug 21 Python
python中单下划线(_)和双下划线(__)的特殊用法
Aug 29 Python
python创建ArcGIS shape文件的实现
Dec 06 Python
python调用API接口实现登陆短信验证
May 10 Python
Python类的继承super相关原理解析
Oct 22 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
基于HTTP长连接的"服务器推"技术的php 简易聊天室
2009/10/31 PHP
PHP中SESSION使用中的一点经验总结
2012/03/30 PHP
基于session_unset与session_destroy的区别详解
2013/06/03 PHP
php实现的DateDiff和DateAdd时间函数代码分享
2014/08/16 PHP
php对关联数组循环遍历的实现方法
2015/03/13 PHP
php实现用于验证所有类型的信用卡类
2015/03/24 PHP
php获取一定范围内取N个不重复的随机数
2016/05/28 PHP
php设计模式之迭代器模式实例分析【星际争霸游戏案例】
2020/04/07 PHP
Javascript miscellanea -display data real time, using window.status
2007/01/09 Javascript
JavaScript CSS修改学习第三章 修改样式表
2010/02/19 Javascript
jQuery+JSON+jPlayer实现QQ空间音乐查询功能示例
2013/06/17 Javascript
javascript break指定标签打破多层循环示例
2014/01/20 Javascript
用JS在浏览器中创建下载文件
2014/03/05 Javascript
jQuery中html()方法用法实例
2014/12/25 Javascript
js验证真实姓名与身份证号是否匹配
2015/10/13 Javascript
Javascript中的数据类型之旅
2015/10/18 Javascript
JS鼠标拖拽实例分析
2015/11/23 Javascript
如何通过js实现图片预览功能【附实例代码】
2016/03/30 Javascript
jQuery绑定事件on()与弹窗的简要概述
2016/04/27 Javascript
关于session和cookie的简单理解
2016/06/08 Javascript
jQuery+ajax读取并解析XML文件的方法
2016/09/09 Javascript
JS无缝滚动效果实现方法分析
2016/12/21 Javascript
详解vue.js之props传递参数
2017/12/12 Javascript
javascript中call,apply,callee,caller用法实例分析
2019/07/24 Javascript
详解关于Vue单元测试的几个坑
2020/04/26 Javascript
对Python中内置异常层次结构详解
2018/10/18 Python
详解用Python实现自动化监控远程服务器
2019/05/18 Python
如何为Python终端提供持久性历史记录
2019/09/03 Python
Python3加密解密库Crypto的RSA加解密和签名/验签实现方法实例
2020/02/11 Python
美国儿童珠宝在线零售商:Loveivy
2019/05/22 全球购物
个人应聘自我评价分享
2013/11/18 职场文书
军训自我鉴定
2014/01/22 职场文书
小学运动会入场式解说词
2014/02/18 职场文书
建筑安全员岗位职责
2014/03/13 职场文书
优秀党员先进事迹材料2016
2016/02/29 职场文书
使用CSS设置滚动条样式
2022/01/18 HTML / CSS