简体   繁体   English

JSON文件有效,但我无法解析

[英]JSON file is valid but I can't parse it

The valid JSON file below: 有效的JSON文件如下:

{
"response": {
    "player_count": 6453,
    "result": 1
}
}

When I use the console.log(req.responseText) command I get: 当我使用console.log(req.responseText)命令时,我得到:

{
"response": {
    "player_count": 6453,
    "result": 1
}
}

When using the following from my Node server (by sending the JSON file to it) All I get is [object Object] when using the code below: This is where the problem lies I think. 当从我的节点服务器上使用以下内容时(通过向其发送JSON文件),在使用下面的代码时,我得到的只是[对象对象]:我认为这就是问题所在。

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>

And it also just comes up with [object Object]. 并且它也只是带有[object Object]。 Any help on this would be greatly appreciated (please no links to other sites unless they have thorough examples of fixing this). 在此方面的任何帮助将不胜感激(除非其他站点有完整的示例来解决此问题,请不要链接到其他站点)。

This problem doesn't happen if it is just this JSON file: 如果只是这个JSON文件,就不会发生此问题:

 {
    "player_count": 6453,
    "result": 1
 }

I think you're just iterating the wrong part of the JSON. 我认为您只是在遍历JSON的错误部分。 What happens if you change your for in loop to this? 如果将for in循环更改for in会发生什么?

for (var p in req.body.response){
    params.push({'name':p,'value':req.body.response[p]})
}

Try below code 试试下面的代码

var esr = { 
   "response":{  
      "player_count":6453,
      "result":1
   }
}

var responseText = (JSON.parse(JSON.stringify(esr)));
alert(responseText.response.player_count); //out put 6453

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM