首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >IPython笔记本中漂亮的JSON格式

IPython笔记本中漂亮的JSON格式
EN

Stack Overflow用户
提问于 2013-09-18 21:05:04
回答 5查看 47.9K关注 0票数 57

有没有一种现有的方法可以让json.dumps()输出在ipython notebook中显示为“漂亮的”格式的JSON?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-09-18 21:08:03

json.dumps有一个indent参数,打印结果应该足够了:

代码语言:javascript
复制
print(json.dumps(obj, indent=2))
票数 85
EN

Stack Overflow用户

发布于 2018-07-31 23:09:26

这可能与OP所要求的稍有不同,但是您可以使用IPython.display.JSON以交互方式查看JSON/dict对象。

代码语言:javascript
复制
from IPython.display import JSON
JSON({'a': [1, 2, 3, 4,], 'b': {'inner1': 'helloworld', 'inner2': 'foobar'}})

编辑:这适用于H2和JupyterLab,但不适用于Jupyter Notebook或IPython终端。

Hydrogen内幕

票数 58
EN

Stack Overflow用户

发布于 2016-05-10 03:47:15

代码语言:javascript
复制
import uuid
from IPython.display import display_javascript, display_html, display
import json

class RenderJSON(object):
    def __init__(self, json_data):
        if isinstance(json_data, dict):
            self.json_str = json.dumps(json_data)
        else:
            self.json_str = json_data
        self.uuid = str(uuid.uuid4())

    def _ipython_display_(self):
        display_html('<div id="{}" style="height: 600px; width:100%;"></div>'.format(self.uuid), raw=True)
        display_javascript("""
        require(["https://rawgit.com/caldwell/renderjson/master/renderjson.js"], function() {
        document.getElementById('%s').appendChild(renderjson(%s))
        });
        """ % (self.uuid, self.json_str), raw=True)

要以可折叠格式输出数据:

代码语言:javascript
复制
RenderJSON(your_json)

从此处粘贴的副本:https://www.reddit.com/r/IPython/comments/34t4m7/lpt_print_json_in_collapsible_format_in_ipython/

Github:https://github.com/caldwell/renderjson

票数 36
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18873066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档