json到html表表示

json2html的Python项目详细描述


JSON转换为人类可读的HTML Table表示的python包装器。

Latest VersionDownloadsBuild

功能

  • 用户友好的小报fomat,易于阅读和分享。
  • 如果键的值是对象数组,并且所有键都是相同的(键的值是list的dict),则默认情况下模块将成为club。例如
input={"sampleData": [{"a":1, "b":2, "c":3
        }, {"a":5, "b":6, "c":7
        }]}

will create only one row combining the results. This feature can be turned off by explicitly passing an argument ``clubbing= False``.
  • 生成的表可以显式地提供一些attributes。例如,给出idclass或任何data-*属性。
  • 与python 3兼容

现场演示

Click here在线演示。

有效参数列表

json2html.convert-模块的convert方法接受以下参数:

ArgumentDescription
jsona valid JSON; This can either be a string in valid JSON format or a python object that is either dict-like or list-like at the top level.
table_attributese.g. pass id=”info-table” or class=”bootstrap-class”/data-* to apply these attributes to the generated table
clubbingturn on[default]/off clubbing of list with same keys of a dict / Array of objects with same key
encodeturn on/off[default] encoding of result to escaped html, compatible with any browser
escapeturn on[default]/off escaping of html tags in text nodes (prevents XSS attacks in case you pass untrusted data to json2html)

安装

$ pip install json2html

或者,下载[此处](https://github.com/softvar/json2html/releases)并在将目录更改为/json2html

后运行python setup.py install

示例用法

示例1:基本用法

fromjson2htmlimport*input={"name":"json2html","description":"Converts JSON to HTML tabular representation"}json2html.convert(json=input)

输出:

<table border="1"><tr><th>name</th><td>json2html</td></tr><tr><th>description</th><td>converts JSON to HTML tabular representation</td></tr></table>
namejson2html
descriptionConverts JSON to HTML tabular representation

示例2:设置表的自定义属性

fromjson2htmlimport*input={"name":"json2html","description":"Converts JSON to HTML tabular representation"}json2html.convert(json=input,table_attributes="id=\"info-table\" class=\"table table-bordered table-hover\"")

输出:

<table id="info-table"class="table table-bordered table-hover"><tr><th>name</th><td>json2html</td></tr><tr><th>description</th><td>Converts JSON to HTML tabular representation</td></tr></table>

示例3:使用相同的键:对象数组

fromjson2htmlimport*input={"sample":[{"a":1,"b":2,"c":3},{"a":5,"b":6,"c":7}]}json2html.convert(json=input)

输出:

<table border="1"><tr><th>sample</th><td><table border="1"><thead><tr><th>b</th><th>c</th><th>a</th></tr></thead><tbody><tr><td>2</td><td>3</td><td>1</td></tr><tr><td>6</td><td>7</td><td>5</td></tr></tbody></table></td></tr></table>
acb
132
576

示例4:对象数组的不同键的每一行

fromjson2htmlimport*input={"sample":[{"a":1,"b":2,"c":3},{"1a1":5,"1b1":6,"c":7}]}json2html.convert(json=input)

输出:

<table border="1"><tr><th>sample</th><td><ul><li><table border="1"><tr><th>a</th><td>1</td></tr><tr><th>c</th><td>3</td></tr><tr><th>b</th><td>2</td></tr></table></li><li><table border="1"><tr><th>1b1</th><td>6</td></tr><tr><th>c</th><td>7</td></tr><tr><th>1a1</th><td>5</td></tr></table></li></ul></td></tr></table>

示例5:[源:json.org/example]

fromjson2htmlimport*input={"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}json2html.convert(json=input)

输出:

<table border="1"><tr><th>glossary</th><td><table border="1"><tr><th>GlossDiv</th><td><table border="1"><tr><th>GlossList</th><td><table border="1"><tr><th>GlossEntry</th><td><table border="1"><tr><th>GlossDef</th><td><table border="1"><tr><th>GlossSeeAlso</th><td><ul><li>GML</li><li>XML</li></ul></td></tr><tr><th>para</th><td>A meta-markup language, used to create markup languages such as DocBook.</td></tr></table></td></tr><tr><th>GlossSee</th><td>markup</td></tr><tr><th>Acronym</th><td>SGML</td></tr><tr><th>GlossTerm</th><td>Standard Generalized Markup Language</td></tr><tr><th>Abbrev</th><td>ISO 8879:1986</td></tr><tr><th>SortAs</th><td>SGML</td></tr><tr><th>ID</th><td>SGML</td></tr></table></td></tr></table></td></tr><tr><th>title</th><td>S</td></tr></table></td></tr><tr><th>title</th><td>example glossary</td></tr></table></td></tr></table>

测试

cd test/
python run_tests.py

使用Python2.6、2.7、3.4和3.5进行测试。

贡献者

  1. 米歇尔·穆勒:[@mueller michel](https://github.com/muellermichel
    • 增加了对具有相同键的对象的clubing数组的支持,更具可读性。
    • 添加了对添加自定义表属性的支持。
    • convert现在接受关键字参数“json”的unicode和bytestrings。
    • 现在的输出应该总是以与输入相同的顺序出现。
    • 现在支持json列表(顶级),包括clubing。
    • 现在支持空输入和位置参数进行转换。
    • Python3支持;增加了Python2.6、3.4和3.5的集成测试,这样支持就不会中断。
    • 现在还可以为您执行正确的编码(默认情况下禁用以不破坏向后兼容性)。
    • 现在可以按照尽力而为的原则处理非json对象。
    • 现在默认情况下,在文本节点中转义html以防止xss攻击。
  2. daniel lekic:[@lekic](https://github.com/lekic
    • 修复了一个项目列表呈现不正确的问题。
    • 常规代码清理,修复了所有命名约定和编码标准,以遵守PEP8约定。
  3. 凯尔·史密斯:[@smithk86](https://github.com/smithk86
    • 从对象数组创建表时,向组标题行和内容行添加了AD和TBody标记。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何找到数组索引的位置和最小值?   运行索引时使用java。jsp,我犯了很多错误   java如何在堆上实现heapSort?   java将图像从flask服务器发送到安卓应用程序   java Wildfly无法在会话关闭后自动释放到池的数据源连接   使用ElasticSearch的java JHipster   Java内核32。读取文件错误(字节缓冲无法转换为字节[])   string Java,一个我无法理解的返回方法   来自Java的UDP消息仅在第一次运行时接收   java同步在TimerTask中失败   java如何将两个字体大小不同的文本视图居中   netbeans如何获取java。朗;从类加载器初始化,而不是编译文件*。JAVA   java向JCheckBox的ArrayList添加ItemListeners