json到html表表示
json2html的Python项目详细描述
将JSON转换为人类可读的HTML Table表示的python包装器。
功能
- 用户友好的小报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。例如,给出id、class或任何data-*属性。
- 与python 3兼容
现场演示
Click here在线演示。
有效参数列表
json2html.convert-模块的convert方法接受以下参数:
Argument | Description |
json | a 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_attributes | e.g. pass id=”info-table” or class=”bootstrap-class”/data-* to apply these attributes to the generated table |
clubbing | turn on[default]/off clubbing of list with same keys of a dict / Array of objects with same key |
encode | turn on/off[default] encoding of result to escaped html, compatible with any browser |
escape | turn 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>
name | json2html |
description | Converts 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>
a | c | b |
1 | 3 | 2 |
5 | 7 | 6 |
示例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进行测试。
贡献者
- 米歇尔·穆勒:[@mueller michel](https://github.com/muellermichel)
- 增加了对具有相同键的对象的clubing数组的支持,更具可读性。
- 添加了对添加自定义表属性的支持。
- convert现在接受关键字参数“json”的unicode和bytestrings。
- 现在的输出应该总是以与输入相同的顺序出现。
- 现在支持json列表(顶级),包括clubing。
- 现在支持空输入和位置参数进行转换。
- Python3支持;增加了Python2.6、3.4和3.5的集成测试,这样支持就不会中断。
- 现在还可以为您执行正确的编码(默认情况下禁用以不破坏向后兼容性)。
- 现在可以按照尽力而为的原则处理非json对象。
- 现在默认情况下,在文本节点中转义html以防止xss攻击。
- daniel lekic:[@lekic](https://github.com/lekic)
- 修复了一个项目列表呈现不正确的问题。
- 常规代码清理,修复了所有命名约定和编码标准,以遵守PEP8约定。
- 凯尔·史密斯:[@smithk86](https://github.com/smithk86)
- 从对象数组创建表时,向组标题行和内容行添加了AD和TBody标记。
版权和许可
The MIT license
Copyright (c) 2014-2017 Varun Malhotra
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.