简体   繁体   中英

json2html, python: json data not converted to html

I'm trying to format json data to html using json2html. The json data look like this:

json_obj = [{"Agent Status": "status1", "Last backup": "", "hostId": 1234567, "hostfqdn": "test1.example.com", "username": "user1"}, {"Agent Status": "status2", "Last backup": "", "hostId": 2345678, "hostfqdn": "test2.example.com", "username": "user2"}]

As already reported in post " json2html not a valid json list python ", to make the code works, the json parameter must be a dictionary and not a list, so I'm calling it that way:

json_obj_in_html = json2html.convert(json = { "data" : json_obj })

However it does not format the json data to html only the first level of dictionary { "data" : json_obj }:

print json_obj_in_html
<table border="1"><tr><th>data</th><td>[{"Agent Status": "status1", "Last backup": "", "hostId": 1234567, "hostfqdn": "test1.example.com", "username": "user1"}, {"Agent Status": "status2", "Last backup": "", "hostId": 2345678, "hostfqdn": "test2.example.com", "username": "user2"}]</td></tr></table>

Note that the online convert tool provides the right output: http://json2html.varunmalhotra.xyz/

<table border="1"><tr><th>data</th><td><ul><table border="1"><tr><th>Agent Status</th><td>status1</td></tr><tr><th>Last backup</th><td></td></tr><tr><th>hostId</th><td>1234567</td></tr><tr><th>hostfqdn</th><td>test1.example.com</td></tr><tr><th>username</th><td>user1</td></tr></table><table border="1"><tr><th>Agent Status</th><td>status2</td></tr><tr><th>Last backup</th><td></td></tr><tr><th>hostId</th><td>2345678</td></tr><tr><th>hostfqdn</th><td>test2.example.com</td></tr><tr><th>username</th><td>user2</td></tr></table></ul></td></tr></table>

Any help would be very welcome.

Make sure that json_obj is an array of objects and not a string ( str ).

I put your code to a complete sample:

from json2html import *
json_obj = [{"Agent Status": "status1", "Last backup": "", "hostId": 1234567, "hostfqdn": "test1.example.com", "username": "user1"}, {"Agent Status": "status2", "Last backup": "", "hostId": 2345678, "hostfqdn": "test2.example.com", "username": "user2"}]
json_obj_in_html = json2html.convert(json = { "data" : json_obj })
print json_obj_in_html

With Python 2.7 and json2html 1.0.1 this leads to this result: 在此处输入图片说明

If you receive a result like

<table border="1"><tr><th>data</th><td>[{"Agent Status": "sta...

it is very likely that json_obj is a str and not an array of objects. You can check this by inserting a statement like

print type(json_obj)

before jsonhtml.convert . I assume that type(json_obj) returns a <type 'str'> in your case and that is why the JSON like string appears in your html. To get it right you have to modify your code in that way that type(json_obj) returns <type 'list'> .

My list of dictionaries anomaly_list was already in a json format, so trying to convert it using json.dumps(anomaly_list, sort_keys=True) was turning into a string, which was not what I wanted. I solved the issue by leaving my list of dictionaries as it is and this code now works:

json_obj_in_html = ''
for j in anomalies_list:
    json_obj_in_html += json2html.convert(json = j)

It outputs what I wanted.

@gus42: thanks, your feedback made me understand where the real pb was.

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