[英]json2html from json file (python on server side)
I have the python code where I pass the json file 我有传递json文件的python代码
def home():
with open('file.json', 'a+') as f:
return render_template('index.html', json_data=f.read())
The file look like this 该文件看起来像这样
{"hosts": [{"shortname": "serv1", "ipadr": "10.0.0.1", "longname": "server1"}, {"shortname": "serv2", "ipadr": "10.0.0.2", "longname": "server2"}]}
On the client side, I wrote this code 在客户端,我编写了这段代码
<table id="placar" class="table table-condensed table-bordered">
<thead>
<tr>
<th>shortname</th>
<th>longname</th>
<th>ipadress</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script>
var data = {{ json_data }}
var transform = {
tag: 'tr',
children: [{
"tag": "td",
"html": "${shortname}"
}, {
"tag": "td",
"html": "${ipadr}"
}, {
"tag": "td",
"html": "${longname}"
}]
};
$('#placar > tbody ').json2html(data, transform);
</script>
But it doesn't work with my file, if write the simple array it works perfectly. 但这不适用于我的文件,如果编写简单的数组,则效果很好。 Can anyone say what I did wrong, pass the file or create a table?
谁能说我做错了什么,传递文件或创建表?
please try 请试试
import json
def home():
with open('file.json', 'a+') as f:
return render_template('index.html', json.dumps(f.read()))
You're gonna want to transform the data.hosts rather than just the data object like so 您将要转换data.hosts而不只是像这样的数据对象
$('#placar > tbody ').json2html(data.hosts, transform);
so all together you'll have 所以你们在一起
var data = {"hosts": [{"shortname": "serv1", "ipadr": "10.0.0.1", "longname": "server1"}, {"shortname": "serv2", "ipadr": "10.0.0.2", "longname": "server2"}]};
var transform = {
tag: 'tr',
children: [{
"tag": "td",
"html": "${shortname}"
}, {
"tag": "td",
"html": "${ipadr}"
}, {
"tag": "td",
"html": "${longname}"
}]
};
$('#placar > tbody ').json2html(data.hosts, transform);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.