【发布时间】:2018-06-12 17:41:54
【问题描述】:
我正在尝试循环输入一个 EJS 文件,但总是遇到一个奇怪的错误。首先,我的数据如下所示:
{
"name": "Marina Silva",
"info": [{
"periodBegins": "Sun Apr 14 23:48:36 +0000 2011",
"periodFinishes": "Sun Apr 7 23:48:36 +0000 2011",
"words": [{"text": "evangelico", "size":32},
{"text": "PT", "size": 12},
{"text": "Rede Solidariedade", "size": 34},
{"text": "catolico", "size": 23}]
},
{
"periodBegins": "Sun Apr 7 23:48:36 +0000 2011",
"periodFinishes": "Sun Apr 1 23:48:36 +0000 2011",
"words": [{"text": "Bolsonaro", "size":32},
{"text": "esperanças", "size": 12},
{"text": "deserto", "size": 34},
{"text": "oasis", "size": 23}]
}
]
}
并且从控制器文件中,我按原样返回它,而不对其调用 JSON.stringify()。 在我的视图文件中,我可以使用
将其打印到屏幕上<%= politician %>
但是当我尝试使用
访问“名称”属性时<table>
<% for(var i=0; i < politician.length; i++) { %>
<tr>
<td><%= politician.length %></td>
<td><%= politician[i].name %></td>
</tr>
<% } %>
</table>
我是 1 - 无法获取名称和 2 - 它表示我的对象的大小为 563,这是包含数据的字符串的大小。但是,这对我来说没有意义,因为我认为它应该被视为 JSON 对象而不是字符串。 我尝试在数据对象中使用 JSON.parse(),但我在视图中得到的只是 [object Object]。如何循环访问我的 JSON 数据并将其显示在表格中?
非常感谢!
【问题讨论】:
标签: javascript json node.js express ejs