python - 如何将 JSON 转换为更易于阅读的内容?

标签 python json python-3.x python-requests pretty-print

我的 Python 脚本连接到 API 并获取一些 JSON。
我一直在尝试 pretty-print 、解析、加载、转储,但我还没有弄清楚它们......

现在,当我执行 print(request.json()) 时,我得到了这个:

{'info': {'status': 'OK', 'time': {'seconds': 0.050006151199341, 'human': '50 milliseconds'}},
 'datalist': {'total': 1, 'count': 1, 'offset': 0, 'limit': 3, 'next': 1, 'hidden': 0, 'loaded': True, 'list': [
     {'id': 27862209, 'name': 'Fate/Grand Order', 'package': 'com.xiaomeng.fategrandorder',
      'uname': 'komoe-game-fate-go', 'size': 49527668,
      'icon': 'http://pool.img.xxxxx.com/msi8/9b58a48638b480c17135a10810374bd6_icon.png',
      'graphic': 'http://pool.img.xxxxx.com/msi8/3a240b50ac37a9824b9ac99f1daab8c8_fgraphic_705x345.jpg',
      'added': '2017-05-20 10:54:53', 'modified': '2017-05-20 10:54:53', 'updated': '2018-02-12 12:35:51',
      'uptype': 'regular', 'store': {'id': 750918, 'name': 'msi8',
                                     'avatar': 'http://pool.img.xxxxx.com/msi8/c61a8cfe9f68bfcfb71ef59b46a8ae5d_ravatar.png',
                                     'appearance': {'theme': 'grey',
                                                    'description': '❤️ Welcome To Msi8 Store & My Store Will Mostly Be Specialized in Games With OBB File Extension. I Hope You Find What You Are Looking For Here ❤️'},
                                     'stats': {'apps': 20776, 'subscribers': 96868, 'downloads': 25958359}},
      'file': {'vername': '1.14.5', 'vercode': 52, 'md5sum': 'xxxxx', 'filesize': 49527668,
               'path': 'http://pool.apk.xxxxx.com/msi8/com-xiaomeng-fategrandorder-52-27862209-32a264b031d6933514970c43dea4191f.apk',
               'path_alt': 'http://pool.apk.xxxxx.com/msi8/alt/Y29tLXhpYW9tZW5nLWZhdGVncmFuZG9yZGVyLTUyLTI3ODYyMjA5LTMyYTI2NGIwMzFkNjkzMzUxNDk3MGM0M2RlYTQxOTFm.apk',
               'malware': {'rank': 'UNKNOWN'}},
      'stats': {'downloads': 432, 'pdownloads': 452, 'rating': {'avg': 0, 'total': 0},
                'prating': {'avg': 0, 'total': 0}}, 'has_versions': False, 'obb': None,
      'xxxxx': {'advertising': False, 'billing': False}}]}}

但我希望它看起来像这样: enter image description here

最佳答案

>>> import json
>>> a={"some":"json", "a":{"b":[1,2,3,4]}}
>>> print(json.dumps(a, indent=4, sort_keys=True))
{
    "a": {
        "b": [
            1,
            2,
            3,
            4
        ]
    },
    "some": "json"
}

关于python - 如何将 JSON 转换为更易于阅读的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55289371/

相关文章:

python - cv2.cornerSubPix() 函数返回 None 值

python - 为什么无法从 Python 的 apply 函数内部访问其他变量?

java - 将 json 格式的参数数组传递给 SOAP Web 服务?

php - 在 php 中从 json 创建多维数组

python - 删除列表中字符串的所有扩展名

python - 在没有引号的情况下写入 CSV,escapcehar 错误

Python 双下划线修改

python - 如何从字符串列表中删除\n

python - 只从字符串 python 3 中删除一个字符一次

java - Aws Json 异常 - JSONObject 文本必须以 '{' 开头(位于 1 [字符 2 第 1 行])