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 相关文章推荐
pytyon 带有重复的全排列
Aug 13 Python
python自动安装pip
Apr 24 Python
python+VTK环境搭建及第一个简单程序代码
Dec 13 Python
Python实现读取及写入csv文件的方法示例
Jan 12 Python
Python使用pyshp库读取shapefile信息的方法
Dec 29 Python
python利用多种方式来统计词频(单词个数)
May 27 Python
30秒学会30个超实用Python代码片段【收藏版】
Oct 15 Python
python manage.py runserver流程解析
Nov 08 Python
django实现HttpResponse返回json数据为中文
Mar 27 Python
keras 权重保存和权重载入方式
May 21 Python
python中threading和queue库实现多线程编程
Feb 06 Python
教你怎么用python爬取爱奇艺热门电影
May 20 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
推荐几部必看的DC动画电影
2020/03/03 欧美动漫
第一节--面向对象编程
2006/11/16 PHP
PHP中创建和验证哈希的简单方法实探
2015/07/06 PHP
限制复选框的最大可选数
2006/07/01 Javascript
JAVASCRIPT  THIS详解 面向对象
2009/03/25 Javascript
javascript常见用法总结
2014/05/22 Javascript
JavaScript字符串对象toUpperCase方法入门实例(用于把字母转换为大写)
2014/10/17 Javascript
JavaScript中数据结构与算法(二):队列
2015/06/19 Javascript
轻松实现JavaScript图片切换
2016/01/12 Javascript
BootstrapTable与KnockoutJS相结合实现增删改查功能【一】
2016/05/10 Javascript
基于jQuery插件实现点击小图显示大图效果
2016/05/11 Javascript
js html css实现复选框全选与反选
2016/10/09 Javascript
详解Javascript百度地图接口开发文档中的类和方法
2017/02/07 Javascript
Vue结合SignalR实现前后端实时消息同步
2017/09/19 Javascript
详解vue静态资源打包中的坑与解决方案
2018/02/05 Javascript
JS用最简单的方法实现四舍五入
2019/08/27 Javascript
JavaScript数组类型Array相关的属性与方法详解
2020/09/08 Javascript
[02:28]PWL开团时刻DAY3——Ink Ice与DeMonsTer之间的勾心斗角
2020/11/03 DOTA
Python使用smtp和pop简单收发邮件完整实例
2018/01/09 Python
Python爬虫框架Scrapy常用命令总结
2018/07/26 Python
Python打包方法Pyinstaller的使用
2018/10/09 Python
利用Python复制文件的9种方法总结
2019/09/02 Python
python 图像的离散傅立叶变换实例
2020/01/02 Python
Django中的session用法详解
2020/03/09 Python
python爬虫判断招聘信息是否存在的实例代码
2020/11/20 Python
Python中Qslider控件实操详解
2021/02/20 Python
美国开幕式潮店:Opening Ceremony
2018/02/10 全球购物
请写出char *p与"零值"比较的if语句
2014/09/24 面试题
兼职学生的自我评价
2013/11/24 职场文书
班级入场式解说词
2014/02/01 职场文书
趣味体育活动方案
2014/02/08 职场文书
2014年小学教导处工作总结
2014/12/19 职场文书
2015大学生党员自我评价范文
2015/03/03 职场文书
民警忠诚教育心得体会
2016/01/23 职场文书
小学信息技术教学反思
2016/02/16 职场文书
springboot+VUE实现登录注册
2021/05/27 Vue.js