javascript - JSON 文件有效,但无法解析它

标签 javascript json node.js parsing handlebars.js

下面的有效 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/

相关文章:

node.js - 开始使用 material-ui

node.js - ExpressJS使用中间件授权用户?

javascript - 选择并显示 JSON 数组中选定的值范围

JavaScript 堆栈 - Web 服务器和 API 服务器 : together or separate?

json - Keycloak:验证访问 token 并获取 keycloak ID

jQuery 访问 JSON 元素

javascript - 如何替换html文件中多个标签的内容? [ Node .js]

javascript - 在 Javascript 中使用授权 header

javascript - 如何在使用 javascript 样式组件的 react 中使用三元运算符?

.net - 通用 WCF JSON 反序列化