JSON 文件有效但我无法解析它
JSON file is valid but I can't parse it
下面的有效 JSON 文件:
{
"response": {
"player_count": 6453,
"result": 1
}
}
当我使用 console.log(req.responseText) 命令时,我得到:
{
"response": {
"player_count": 6453,
"result": 1
}
}
当从我的节点服务器使用以下内容时(通过向其发送 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
}
试试下面的代码
var esr = {
"response":{
"player_count":6453,
"result":1
}
}
var responseText = (JSON.parse(JSON.stringify(esr)));
alert(responseText.response.player_count); //out put 6453
我认为您只是迭代了 JSON 的错误部分。如果将 for in
循环更改为此会发生什么情况?
for (var p in req.body.response){
params.push({'name':p,'value':req.body.response[p]})
}
下面的有效 JSON 文件:
{
"response": {
"player_count": 6453,
"result": 1
}
}
当我使用 console.log(req.responseText) 命令时,我得到:
{
"response": {
"player_count": 6453,
"result": 1
}
}
当从我的节点服务器使用以下内容时(通过向其发送 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
}
试试下面的代码
var esr = {
"response":{
"player_count":6453,
"result":1
}
}
var responseText = (JSON.parse(JSON.stringify(esr)));
alert(responseText.response.player_count); //out put 6453
我认为您只是迭代了 JSON 的错误部分。如果将 for in
循环更改为此会发生什么情况?
for (var p in req.body.response){
params.push({'name':p,'value':req.body.response[p]})
}