如何将json响应传递给jade并循环结果

时间:2016-03-08 06:12:37

标签: json node.js express pug

我正在尝试使用node.js应用程序来发布http请求。响应是json格式,我需要在玉页中显示它。当我尝试这样做时,我没有得到正确的JSON响应。

app.js

var request = require('request');
var options = {
    url: 'my URL goes here ',
    method: 'POST',
    headers: {
          'Content-Type': 'application/json'
      },
    auth: {
    user : 'USERID',
    pass : '****'
          },
    body: JSON.stringify("1": "T1111")
}
request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
           res.render('CRUD/singleselect', {
            title: 'Select',
            name: 'You!',
            result: body
  });
    }
    else
    {
        console.log(response.statusCode);
        req.flash('errors', { msg: 'Error during select' });
    }
})

实际回应是

{"ResultSet Output":[{"EMP_ID":"T1111","EMP_NAME":"ABC","DESIGNATION":"ENGINEER","REGISTRATION":"YES"}],"StatusDescription":"Execution Successful","StatusCode":200}

singleselect.jade

extends ../layout

block content
  .page-header
    table
      thead
    tbody
     table.table.table-striped.table-bordered.table-hover.table-condensed
      tr
        th EMPID
        th EMPNAME
        th DESIGNATION
         - var cnt = 1;
      each key, ind in result
        td= (cnt++)+'.'
        td= key
        tr

当我这样做时,我得到的回应是这样的

1.  {
2.  "
3.  R
4.  e
5.  s
6.  u
7.  l
8.  t
9.  S
10. e
11. t
12. 
13. O
14. u
15. t
16. p
17. u
18. t
19. "
20. :
21. [
22. {
23. "

我想要一个像这样的输出

S.No EMPID EMPNAME DESIGNATION
1     T1111 ABC   ENGINEER  

请帮忙

1 个答案:

答案 0 :(得分:0)

我认为问题在于您正在设置字符串响应。你应该试试这个:(当你直接循环遍历这个属性时,将它解析为json并选择"ResultSet Output"results

       res.render('CRUD/singleselect', {
        title: 'Select',
        name: 'You!',
        result: JSON.parse(body)['ResultSet Output']
       });

我是handelbars人,但我强烈关注应该工作。

extends ../layout

block content
  .page-header
    table
      thead
    tbody
     table.table.table-striped.table-bordered.table-hover.table-condensed
      tr
        th EMPID
        th EMPNAME
        th DESIGNATION
         - var cnt = 1;
      each record, index in result
        td= record.EMP_ID
        td= record.EMP_NAME
        td= record.DESIGNATION
        tr

希望这有帮助。