我正在尝试在我的jade模板中使用json对象,由于某种原因,我似乎无法解开它,所以我循环的每个项目都是看到每个字符而不是对象变量。我已经尝试了我能想到的一切,但无济于事。请帮忙。
这是我的js:
db.view('dataset/byUsername', { key: req }, function (err, doc)
{
datasets = doc;
var config = {
'user': req,
'datasets': jQuery.parseJSON(datasets),
'dashboards': []
};
res.render('welcome', { config: jQuery.parseJSON(config) });
});
我的json数据集对象如下所示:
:[{"id":"13788ea7cfd01e07bd5b393474036e28","key":"mike.merritt-holmes@bigdatapartnership.com","value":{"_id":"13788ea7cfd01e07bd5b393474036e28","_rev":"1-81ab52488eae6dbef8265e4459cf6b96","jsonType":"datasetconfig","username":"mike.merritt-holmes@bigdatapartnership.com","filename":"2321313123.txt","size":"1312313213","created_at":"2013-06-16T23:02:01.680Z","updated_at":"2013-06-16T23:02:01.680Z"}},{"id":"c1cf81b1fada50867f31bc11e952fe41","key":"mike.merritt-holmes@bigdatapartnership.com","value":{"_id":"c1cf81b1fada50867f31bc11e952fe41","_rev":"1-7153e8e92efe3dffd80a2ec363bd3fc2","jsonType":"datasetconfig","username":"mike.merritt-holmes@bigdatapartnership.com","filename":"2321313123.txt","size":"1312313213","created_at":"2013-06-16T23:08:25.988Z","updated_at":"2013-06-16T23:08:25.988Z"}}]
我的玉模板看起来像这样:
table.table.table-hover.table-bordered.table-striped
thead
tr
th #
th Dataset
tbody(data-provides="rowlink")
if (config.datasets)
for item,key in config.datasets
tr.rowlink
td(href='#me')
a(href='/dashboard/1') item.username
td
a(href='/dashboard/1') item.filename
我尝试了#{config.datasets},但这会产生预期的字符ILLEGAL错误。
答案 0 :(得分:0)
尝试美化你的json以获得最佳阅读效果。
JSON:
{"config":[
{
"id": "13788ea7cfd01e07bd5b393474036e28",
"key": "mike.merritt-holmes@bigdatapartnership.com",
"value": {
"_id": "13788ea7cfd01e07bd5b393474036e28",
"_rev": "1-81ab52488eae6dbef8265e4459cf6b96",
"jsonType": "datasetconfig",
"username": "mike.merritt-holmes@bigdatapartnership.com",
"filename": "2321313123.txt",
"size": "1312313213",
"created_at": "2013-06-16T23:02:01.680Z",
"updated_at": "2013-06-16T23:02:01.680Z"
}
},
{
"id": "c1cf81b1fada50867f31bc11e952fe41",
"key": "mike.merritt-holmes@bigdatapartnership.com",
"value": {
"_id": "c1cf81b1fada50867f31bc11e952fe41",
"_rev": "1-7153e8e92efe3dffd80a2ec363bd3fc2",
"jsonType": "datasetconfig",
"username": "mike.merritt-holmes@bigdatapartnership.com",
"filename": "2321313123.txt",
"size": "1312313213",
"created_at": "2013-06-16T23:08:25.988Z",
"updated_at": "2013-06-16T23:08:25.988Z"
}
}
]}
玉:
table.table.table-hover.table-bordered.table-striped
thead
tr
th #
th Dataset
tbody(data-provides="rowlink")
if (config)
each data in config
tr.rowlink
td(href='#me')
a(href='/dashboard/1') #{data.value.username}
td
a(href='/dashboard/1') #{data.value.filename}
td
a(href='/dashboard/1') #{data.value._id}
td
a(href='/dashboard/1') #{data.value._rev}
td
a(href='/dashboard/1') #{data.value.created_at}
td
a(href='/dashboard/1') #{data.id}
td
a(href='/dashboard/1') #{data.key}
渲染:
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>Dataset</th>
</tr>
</thead>
<tbody data-provides="rowlink">
<tr class="rowlink">
<td href="#me"><a href="/dashboard/1">mike.merritt-holmes@bigdatapartnership.com</a></td>
<td><a href="/dashboard/1">2321313123.txt</a></td>
<td><a href="/dashboard/1">13788ea7cfd01e07bd5b393474036e28</a></td>
<td><a href="/dashboard/1">1-81ab52488eae6dbef8265e4459cf6b96</a></td>
<td><a href="/dashboard/1">2013-06-16T23:02:01.680Z</a></td>
<td><a href="/dashboard/1">13788ea7cfd01e07bd5b393474036e28</a></td>
<td><a href="/dashboard/1">mike.merritt-holmes@bigdatapartnership.com</a></td>
</tr>
<tr class="rowlink">
<td href="#me"><a href="/dashboard/1">mike.merritt-holmes@bigdatapartnership.com</a></td>
<td><a href="/dashboard/1">2321313123.txt</a></td>
<td><a href="/dashboard/1">c1cf81b1fada50867f31bc11e952fe41</a></td>
<td><a href="/dashboard/1">1-7153e8e92efe3dffd80a2ec363bd3fc2</a></td>
<td><a href="/dashboard/1">2013-06-16T23:08:25.988Z</a></td>
<td><a href="/dashboard/1">c1cf81b1fada50867f31bc11e952fe41</a></td>
<td><a href="/dashboard/1">mike.merritt-holmes@bigdatapartnership.com</a></td>
</tr>
</tbody>
</table>