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

问题描述 投票:2回答:1

我的Python脚本连接到API并获得一些JSON。 我一直在尝试漂亮的印刷,解析,加载,转储,但我还没想出来......

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

python json python-3.x python-requests pretty-print
1个回答
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"
}
© www.soinside.com 2019 - 2024. All rights reserved.