Javascript 如何解析RESTAPI json响应并将数据插入Jade模板(Node.js+;express)?
所以,我需要解析node.js+express中的json响应,并将数据插入jade文件。我在西纳特拉做这件事很容易,但在这里。。 响应格式如下:Javascript 如何解析RESTAPI json响应并将数据插入Jade模板(Node.js+;express)?,javascript,json,node.js,express,pug,Javascript,Json,Node.js,Express,Pug,所以,我需要解析node.js+express中的json响应,并将数据插入jade文件。我在西纳特拉做这件事很容易,但在这里。。 响应格式如下: { "status": "200", "name": "", "port": "7777", "playercount": "4", "players": "name, of, player" } Express的方法允许您传入本地模板变量,并在模板中使用它们。例如: app.route('/', function (req,
{
"status": "200",
"name": "",
"port": "7777",
"playercount": "4",
"players": "name, of, player"
}
Express的方法允许您传入本地模板变量,并在模板中使用它们。例如:
app.route('/', function (req, res) {
// Your code to get the response, and for example's sake, I'll say it's assigned to 'view_data'.
if (typeof view_data === 'string') {
// If you know for sure if your data is going to be an object or a string,
// you can leave the if statement out, and instead just parse it (or not if
// it's already an object.
view_data = JSON.parse(view_data);
}
res.render('template', view_data);
});
并且在模板内。jade
h1 This is #{name}
pre= status
p #{playercount} players online
数据可以是任何JSON对象,因此如果以文本形式返回响应,则可以使用将其转换为JSON对象。var obj=
JSON.parse(JSON_str)
?您好,欢迎来到StackOverflow!我建议你在网站上搜索类似的问题。解析JSON是一个已经被问过并回答过多次的问题。我还发现它似乎和你的非常相似。