无法解析Flask Jinja temp中的JSON对象

2024-05-13 06:10:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的JSON数据:

{"thisdata": ["/home/fyp/Desktop/AVA/AVA-STORAGE", "Network has already been configured since nexpose-pc is in virtualbox! ...", "/home/fyp/Desktop/AVA/AVA-APP/RunGUI", "ubuntu-trusty\t192.168.0.21", "", "/home/fyp/Desktop/AVA/AVA-APP/RunGUI", "centos-7\t192.168.0.22", "", "/usr/sbin/apache2", "[sudo] password for fyp: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message", "(' * Restarting web server apache2\\n   ...done.\\n', None)", "/usr/local/packer/packer", "/usr/bin/virtualbox", "centos 8 is not configured, going to install distribution", "Traceback (most recent call last):", "File \"../RunCMD/ava.py\", line 684, in <module>", "createvm.runPacker()", "File \"../RunCMD/ava.py\", line 124, in runPacker", "packer_tmp + '/' + self.Hostname + '_template.json')", "File \"/usr/lib/python2.7/shutil.py\", line 82, in copyfile", "with open(src, 'rb') as fsrc:", "IOError: [Errno 2] No such file or directory: 'TEMPLATES/redhat/template.json'"]}

我试图在我的Flask应用程序的模板中解析它,但是我似乎不知道如何去做。在

注意:如果不使用jsonify(),我使用json.dumps()并将JSON发送到网页,我可以打印整个JSON数据块,但不能格式化。在

这是我的主应用程序:

^{pr2}$

如您所见,我尝试运行一个脚本,然后使用JSON格式在文件中返回一些行,如上图所示。在

但是我不知道如何解析JSON,我有一种感觉我没有以正确的方式发送JSON,而且我也不知道如何在Jinja模板中呈现JSON。在

我尝试过在模板中打印{{ senddata }},但它没有显示任何内容。在


Tags: 数据inpy模板jsonhomeusrline
1条回答
网友
1楼 · 发布于 2024-05-13 06:10:23

我所要做的只是解析列表而不是json,因为我的主要动机是以div形式显示文件内容。在模板中,我只是呈现列表:

            data_string = []
        with open(logfile, 'r') as outfile:
            for line in outfile:
                data_string.append(line.strip())

        return render_template('index.html', form=manual, showouput='showouput', senddata=data_string)


{% if senddata %}
{% for n in senddata%}
    <div>{{ n }}</div>
{%endfor%}
{% endif %}

相关问题 更多 >