简体   繁体   English

JSON2HTML:无效的JSON列表python

[英]JSON2HTML: Not a valid JSON list python

I have a piece of JSON in a file I would like to convert to HTML. 我要转换为HTML的文件中包含一个JSON。 I seen online there is a tool called json2html for python which takes care of this for me. 我在网上看到有一个名为json2html的python工具,它可以帮我解决这个问题。

[{
    "name": "Steve",
    "timestampe": "2016-07-28 10:04:15",
    "age": 22
}, 
{
    "name": "Dave",
    "timestamp": "2016-07-28 10:04:15",
    "age": 34
}]

Above is my JSON, when using the online converter tool - http://json2html.varunmalhotra.xyz/ it works great and produces a nice table for me. 上面是我的JSON,使用在线转换器工具-http://json2html.varunmalhotra.xyz/时,它的效果很好,并为我生成了一个不错的表。

However when I install the library using pip and run the following: 但是,当我使用pip安装库并运行以下命令时:

_json = [{
    "name": "Steve",
    "timestampe": "2016-07-28 10:04:15",
    "age": 22
}, 
{
    "name": "Dave",
    "timestamp": "2016-07-28 10:04:15",
    "age": 34
}]

print json2html.convert(json=_json)

I get an error 我得到一个错误

 File "/root/.pyenv/versions/venv/lib/python2.7/site-packages/json2html/jsonconv.py", line 162, in iterJson
raise Exception('Not a valid JSON list')
 Exception: Not a valid JSON list

I even ran the json through http://jsonlint.com/ and it came back as valid JSON. 我什至通过http://jsonlint.com/运行json,并将其作为有效JSON返回。

I was wondering if anyone would have a fix for this, or could point me in the right direction on how to solve this. 我想知道是否有人可以解决此问题,或者可以为我指出如何解决此问题的正确方向。 I can't find much documentation on this library. 我在该库中找不到太多文档。

For reference this is the link to the pypi library - https://pypi.python.org/pypi/json2html 作为参考,这是链接到的PyPI库- https://pypi.python.org/pypi/json2html

Any help would be appreciated, thanks in advance! 任何帮助将不胜感激,在此先感谢!

parameter json must be a dictionary object and you pass a list. 参数json必须是字典对象,并且您传递一个列表。 try this: 尝试这个:

_json = { "data" : [{"name": "Steve",
    "timestampe": "2016-07-28 10:04:15",
    "age": 22
}, 
{
    "name": "Dave",
    "timestamp": "2016-07-28 10:04:15",
    "age": 34
}]
}
print json2html.convert(json=_json)

尝试使用json.loads()设置_json的值,如此答案所示- 在python中将json转换为html表

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

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