如何在Node / Jade / Express视图中迭代和解析JSON数据? [英] How to iterate and parse JSON data in Node/Jade/Express views?

查看:312
本文介绍了如何在Node / Jade / Express视图中迭代和解析JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Node很新,但仍然在理解其使用和权力的开始阶段。我很抱歉,如果这个问题是非常简单或天真的问题 - 我在进行调查之前做了适当的研究。



在我的express js应用程序中,我向远程API发送了一个GET请求并正确收到相应的JSON数据作为回报。然后我将其传递给我的观点(Jade),目前有能力打印JSON的字符串形式。这一切都可以正常工作。



我拥有的JSON数据是一组具有相关领域的俱乐部的人员/成员。我想抓住所有的人,并在一个组织良好的表格中显示他们的名字和相关信息。



所以我的问题是,解析JSON数据的最佳方法是什么,以便我可以在视图中访问名称和相关信息?



我的请求看起来像这样,数据是发送到视图的JSON数据:

  res.render('standings.jade',{localals:{
data:body,
title:'Team Member Information'
}
}) ;

我的简单视图看起来像:

  h2 = title 
p = data

已经用数据,数据,[成员],数据[成员],数据[成员]等无法使用愚弄。当然,我错过了一些愚蠢的事情我对成员列表感兴趣,返回的JSON如下所示:

  {
club:
{id:1,name:这是一个团队名称},
members:
[{id:2,name },{id:3,name:Another Name},{id:4,name:More Names},{id:5,name }]
}

提前感谢

解决方案

要处理JSON数据,请使用 express.bodyParser()中间件。这将使您在路由回调中可用的 req.body 对象可用。在路线处理函数中,将 req.body 作为视图的本地人的一部分,将该数据提供给视图。

  app.post(/ some / path,express.bodyParser(),function(req,res){
res.render(standings.jade {
本地人:{data:req.body,title:Team Member Information}
});
});

在您看来,您将可以访问 data.members 获取团队成员。

  data.members中的每个成员
p = member.name


I am very new to Node and still in the beginning stages of understanding its use and power. I apologize if this question comes across as very simple or naive - I did due research before asking.

In my express js application, I sent a GET request to a remote API and properly received the appropriate JSON data in return. I then passed it to my views (Jade) and currently have the ability the print the JSON in string form. That all works properly.

The JSON data I have is a group of people/members of a club with associated fields. I want to "grab" all the people and display their names and related information in a nicely organized table.

So my question is, what is the best way to parse the JSON data so I can access the names and related information in the view?

My request looks like this, where data is the JSON being sent to the view:

res.render('standings.jade', { locals: {
          data: body,
          title: 'Team Member Information'
        }
});

And my very simple view looks like:

h2= title
p= data

I've tried fooling around with data.members, data.[members], data.["members"], data[members], etc. to no avail. Surely it is something stupid that I am missing? I am interested in the members listing and the returned JSON looks like this:

{
"club":
{"id":1,"name":"This is a team name"},
"members":
[{"id":2,"name":"Test Name"},{"id":3,"name":"Another Name"},{"id":4,"name":"More Names"},{"id":5,"name":"Cool Person"}]
}

Thanks in advance!

解决方案

To handle JSON data, use the express.bodyParser() middleware. That will make a req.body object available to you in your route callback. In the route handler function, pass req.body as part of the view's locals to make that data available to the view.

app.post("/some/path", express.bodyParser(), function (req, res) {
    res.render("standings.jade", {
        locals: {data: req.body, title: "Team Member Information"}
    });
});

In your view, you will be able to access data.members to get the team members.

each member in data.members
    p= member.name

这篇关于如何在Node / Jade / Express视图中迭代和解析JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆