简体   繁体   English

json2html,python:json数据未转换为html

[英]json2html, python: json data not converted to html

I'm trying to format json data to html using json2html. 我正在尝试使用json2html将json数据格式化为html。 The json data look like this: json数据如下所示:

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: 正如文章“ json2html不是有效的json列表python ”中已经报道的那样,为了使代码有效,json参数必须是字典而不是列表,所以我这样称呼它:

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 }: 但是,它不会将json数据仅格式化为字典{“ 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/ 请注意, 在线转换工具提供了正确的输出: 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 ). 确保json_obj是对象数组,而不是字符串( 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: 使用Python 2.7json2html 1.0.1会导致以下结果: 在此处输入图片说明

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. json_obj很可能是str而不是对象数组。 You can check this by inserting a statement like 您可以通过插入类似

print type(json_obj)

before jsonhtml.convert . 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. 我假设type(json_obj)在您的情况下返回<type 'str'> type(json_obj) <type 'str'> ,这就是为什么JSON之类的字符串出现在您的html中的原因。 To get it right you have to modify your code in that way that type(json_obj) returns <type 'list'> . 为了正确使用它,您必须以这种方式修改代码,使type(json_obj)返回<type 'list'> type(json_obj) <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. 我的词典列表anomaly_list已经是json格式,因此尝试使用json.dumps(anomaly_list, sort_keys=True)进行转换已变成字符串,这不是我想要的。 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. @ gus42:谢谢,您的反馈使我明白了真正的铅在哪里。

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

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