简体   繁体   中英

JSON2HTML: Not a valid JSON list python

I have a piece of JSON in a file I would like to convert to HTML. I seen online there is a tool called json2html for python which takes care of this for me.

[{
    "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.

However when I install the library using pip and run the following:

_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.

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

Any help would be appreciated, thanks in advance!

parameter json must be a dictionary object and you pass a list. 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表

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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