首页 > 解决方案 > Python 在文档字符串中格式化 JSON

问题描述

您好,我正在尝试在 Python 中创建这样的文档字符串:

    def hello_world_request(self, json):
    """
    foo
    Example JSON:
    
    {
        "helloWorld": [
            {
                "firstWord": "Hello",
                "lastWord": "World",
                "comment": "foo"
            }
        ]
    }

    :param json: json object
    """

查看下图,您可以看到文档。是否可以让文档字符串中的每一行在文档中也显示为单独的行?所以它看起来像函数的文档字符串。

文档被压缩示例

标签: pythonjsondocumentation

解决方案


弄清楚了

   """
    foo
     .. code-block:: json

    {
        "helloWorld": [
            {
                "firstWord": "Hello",
                "lastWord": "World",
                "comment": "foo"
            }
        ]
    }

    :param json: json object
    """

推荐阅读