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操作json数据的一个简单例子
Apr 17 Python
Python压缩和解压缩zip文件
Feb 14 Python
python实现对任意大小图片均匀切割的示例
Dec 05 Python
详解pandas.DataFrame中删除包涵特定字符串所在的行
Apr 04 Python
Pycharm使用之设置代码字体大小和颜色主题的教程
Jul 12 Python
对Django中static(静态)文件详解以及{% static %}标签的使用方法
Jul 28 Python
python实现对图片进行旋转,放缩,裁剪的功能
Aug 07 Python
Python如何读取文件中图片格式
Jan 13 Python
python中列表的含义及用法
May 26 Python
解决TensorFlow调用Keras库函数存在的问题
Jul 06 Python
python使用requests库爬取拉勾网招聘信息的实现
Nov 20 Python
Python下opencv使用hough变换检测直线与圆
Jun 18 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
分享最受欢迎的5款PHP框架
2014/11/27 PHP
浅谈本地WAMP环境的搭建
2015/05/13 PHP
PHP设计模式之适配器模式(Adapter)原理与用法详解
2019/12/12 PHP
EasySlider 基于jQuery功能强大简单易用的滑动门插件
2010/06/11 Javascript
jquery里的正则表达式说明
2011/08/03 Javascript
Ajax提交与传统表单提交的区别说明
2014/02/07 Javascript
Jquery中request和request.form和request.querystring的区别
2015/11/26 Javascript
JavaScript设计模式开发中组合模式的使用教程
2016/05/18 Javascript
js创建数组的简单方法
2016/07/27 Javascript
详解angular中如何监控dom渲染完毕
2017/01/03 Javascript
Vue.extend构造器的详解
2017/07/17 Javascript
浅谈JavaScript的innerWidth与innerHeight
2017/10/12 Javascript
详解vue静态资源打包中的坑与解决方案
2018/02/05 Javascript
vue 组件的封装之基于axios的ajax请求方法
2018/08/11 Javascript
angularJS1 url中携带参数的获取方法
2018/10/09 Javascript
elementUI 设置input的只读或禁用的方法
2018/10/30 Javascript
Javascript实现一朵从含苞到绽放的玫瑰
2019/03/30 Javascript
Vue项目打包编译优化方案
2020/09/16 Javascript
JavaScript实现点击出现子菜单效果
2021/02/08 Javascript
Python入门教程之if语句的用法
2015/05/14 Python
Python爬虫实例扒取2345天气预报
2018/03/04 Python
Python拼接字符串的7种方法总结
2018/11/01 Python
简单了解python中的f.b.u.r函数
2019/11/02 Python
python Opencv计算图像相似度过程解析
2019/12/03 Python
Keras 利用sklearn的ROC-AUC建立评价函数详解
2020/06/15 Python
缅甸网上购物:Shop.com.mm
2017/12/05 全球购物
JBL澳大利亚官方商店:扬声器、耳机和音响系统
2018/05/24 全球购物
ONLY瑞典官网:世界知名服装品牌
2018/06/19 全球购物
中国一家综合的外贸B2C电子商务网站:DealeXtreme(DX)
2020/03/10 全球购物
Kipling澳洲官网:购买凯浦林包包
2020/12/17 全球购物
《钱学森》听课反思
2014/03/01 职场文书
中国入世承诺
2014/04/01 职场文书
2015年党员公开承诺书范文
2015/01/22 职场文书
矛盾论读书笔记
2015/06/29 职场文书
实习报告怎么写
2019/06/20 职场文书
MYSQL事务的隔离级别与MVCC
2022/05/25 MySQL