下面的有效 JSON 文件:
{
"response": {
"player_count": 6453,
"result": 1
}
}
当我使用 console.log(req.responseText) 命令时,我得到:
{
"response": {
"player_count": 6453,
"result": 1
}
}
当使用我的 Node 服务器中的以下内容时(通过向其发送 JSON 文件),使用下面的代码时我得到的只是 [object Object]:我认为这就是问题所在。
app.post('/steam-output', function(req,res){
var params = [];
for (var p in req.body){
params.push({'name':p,'value':req.body[p]})
}
console.log(params);
console.log(req.body);
var context = {};
context.dataList = params;
res.render('steam-output', context);
// steam-output is a handlebars file which is what my Node.js server is running
});
//here is the steam-output.handlebars file
<h1></h1>
<ul>
{{#each dataList}}
<li>{{this.name}}: {{this.value}}
{{/each}}
</ul>
它也只是出现了[object Object]。 对此的任何帮助将不胜感激(请不要链接到其他网站,除非他们有解决此问题的完整示例)。
如果只是这个 JSON 文件,则不会出现此问题:
{
"player_count": 6453,
"result": 1
}
最佳答案
我认为您只是迭代了 JSON 的错误部分。如果将 for in
循环更改为这样会发生什么?
for (var p in req.body.response){
params.push({'name':p,'value':req.body.response[p]})
}
关于javascript - JSON 文件有效,但无法解析它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37311777/