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

时间:2019-03-21 21:15:55

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

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

现在,当我做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

1 个答案:

答案 0 :(得分:5)

>>> 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"
}