简体   繁体   English

json2html python lib无法正常工作

[英]json2html python lib is not working

I'm trying to create new json file with my custom json input and converting JSON to HTML format and saving into .html file. 我正在尝试使用我的自定义json输入创建新的json文件,并将JSON转换为HTML格式并保存到.html文件中。 But I'm getting error while generating JSON and HTML file. 但是我在生成JSON和HTML文件时遇到错误。 Please find my below code - Not sure what I'm doing wrong here: 请找到我的下面代码 - 不确定我在这里做错了什么:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from json2html import *
import sys
import json

JsonResponse = {
        "name": "json2html",
        "description": "Converts JSON to HTML tabular representation"
}

def create(JsonResponse):
    #print JsonResponse
    print 'creating new  file'
    try:
        jsonFile = 'testFile.json'
        file = open(jsonFile, 'w')
        file.write(JsonResponse)
        file.close()
        with open('testFile.json') as json_data:
            infoFromJson = json.load(json_data)
            scanOutput = json2html.convert(json=infoFromJson)
            print scanOutput
            htmlReportFile = 'Report.html'
            htmlfile = open(htmlReportFile, 'w')
            htmlfile.write(str(scanOutput))
            htmlfile.close()
    except:
        print 'error occured'
        sys.exit(0)


create(JsonResponse)

Can someone please help me resolve this issue. 有人可以帮我解决这个问题。

Thanks! 谢谢!

First, get rid of your try / except . 首先,摆脱你的try / except Using except without a type expression is almost always a bad idea. 使用except没有类型表达式的情况几乎总是一个坏主意。 In this particular case, it prevented you from knowing what was actually wrong. 在这种特殊情况下,它阻止您了解实际上是错误的。

After we remove the bare except: , we get this useful error message: 删除except:的裸露之后,我们得到了这个有用的错误消息:

Traceback (most recent call last):
  File "x.py", line 31, in <module>
    create(JsonResponse)
  File "x.py", line 18, in create
    file.write(JsonResponse)
TypeError: expected a character buffer object

Sure enough, JsonResponse isn't a character string ( str ), but is a dictionary. 果然, JsonResponse不是字符串( str ),而是字典。 This is easy enough to fix: 这很容易修复:

    file.write(json.dumps(JsonResponse))

Here is a create() subroutine with some other fixes I recommend. 这是一个create()子例程,我建议使用其他一些修复程序。 Note that writing the dumping the JSON followed immediately by loading the JSON is usually silly. 请注意,通过加载JSON立即编写转储JSON后通常是愚蠢的。 I left it in assuming that your actual program does something slightly different. 我假设你的实际程序做了一些略微不同的事情。

def create(JsonResponse):
    jsonFile = 'testFile.json'
    with open(jsonFile, 'w') as json_data:
        json.dump(JsonResponse, json_data)
    with open('testFile.json') as json_data:
        infoFromJson = json.load(json_data)
        scanOutput = json2html.convert(json=infoFromJson)
        htmlReportFile = 'Report.html'
        with open(htmlReportFile, 'w') as htmlfile:
            htmlfile.write(str(scanOutput))

The error is while writing to the JSON file. 写入JSON文件时出错。 Instead of file.write(JsonResponse) you should use json.dump(JsonResponse,file) . 而不是file.write(JsonResponse)你应该使用json.dump(JsonResponse,file) It will work. 它会工作。

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

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