简体   繁体   English

在Python中将JSON转换为HTML表

[英]Converting JSON to HTML table in Python

I've been using the JSON library for Python to get data from JSON files using Python. 我一直在使用Python的JSON库来使用Python从JSON文件中获取数据。

infoFromJson = json.loads(jsonfile)

I fully understand how to work with JSON files in Python. 我完全理解如何在Python中使用JSON文件。 However, I am trying to find a way to format JSON format in a nice way. 但是,我试图找到一种方法来以一种很好的方式格式化JSON格式。

I prefer to convert the JSON into a nested HTML table format. 我更喜欢将JSON转换为嵌套的HTML表格式。

I found json2html for Python, which does exactly what I just described. 我发现了Python的json2html ,它正是我刚才所描述的。 However, it does not actually output anything when I run the script they provide. 但是,当我运行它们提供的脚本时,它实际上并没有输出任何内容。

Has anyone had experience with this tool? 有没有人有这个工具的经验? Or does anyone have suggestions for alternatives? 或者有没有人有替代方案的建议?

Try the following: 请尝试以下方法:

infoFromJson = json.loads(jsonfile)
print json2html.convert(json = infoFromJson)

The result from json2html.convert is a string. json2html.convert的结果是一个字符串。

If you don't have module: 如果你没有模块:

$ pip install json2html

More examples here . 这里有更多例子。

Nowadays it's better to use json2table (at least for Python 3) 现在最好使用json2table(至少对于Python 3)

import json2table
import json

infoFromJson = json.loads(jsonfile)
build_direction = "LEFT_TO_RIGHT"
table_attributes = {"style": "width:100%"}
print(json2table.convert(infoFromJson, 
                         build_direction=build_direction, 
                         table_attributes=table_attributes))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM