简体   繁体   English

在node.js中解析JSON并在jade中显示

[英]Parsing JSON in node.js and displaying in jade

I am trying to parse the json and display the response in a table format in jade. 我试图解析json并以jade的表格格式显示响应。 Request your help in parsing the json and displaying the key and value as two columns. 请求帮助解析json并将键和值显示为两列。

Node.js Node.js的

exports.postMQinput = function(req, res) {
  req.assert('name', 'Queue name field cannot be blank').notEmpty();
  var errors = req.validationErrors();

  if (errors) {
    req.flash('errors', errors);
    return res.redirect('/');
  }

  var options = {
    url: 'URL goes here',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify("my body input comes here")
  }

  request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {            
      res.render('Views/Details', {
        title: 'QUEUE',
        name: 'Welcome',
        result: body
      });
    } else {
      console.log(response.statusCode);
      req.flash('errors', { msg: 'Error during select. Retrying...' });
    }
  })
};

My response looks like this. 我的回答是这样的。

{
  "Response": {
    "data": {
      "length": 123,
      "status": "OPEN"
    }
  }
}

Jade : 玉:

extends ../layout

block content
  .page-header
     h3 MESSAGE QUEUE DETAILS

    table
      thead
    tbody
     table.table.table-striped.table-bordered.table-hover.table-condensed
       tr
        th Value
        th Attribute


      each key, ind in result

        td= "LENGTH"
        td= key.length
        tr

I want table something like this 我想要这样的桌子

Length 123 长度123

Status Open 状态开放

Where the body comes from? 身体来自哪里? Maybe you should run JSON.parse on it? 也许你应该运行JSON.parse

res.render('Views/Details', {
        title: 'QUEUE',
        name: 'Welcome',
        result: JSON.parse(body)
});

You are using in a wrong way the iteration to build the HTML from Jade template, I made this example to show you how to use it in case you got an array of values. 您正在以错误的方式使用迭代来从Jade模板构建HTML,我做了这个示例,向您展示如何使用它,以防您获得一组值。

extends layout

block content
  .page-header
     h3 MESSAGE QUEUE DETAILS

     table.table.table-striped.table-bordered.table-hover.table-condensed
      thead
        tr
          th Value
          th Attribute
      tbody
        for res in result
          for obj in Object.keys(res)
            tr
              td= res[obj]
              td= obj

Just in case that you are having a problem with the way you are sending the JSON, take a look to this example: 如果您遇到发送JSON的方式有问题,请查看此示例:

var express = require('express');
var path = require('path');
var http = require('http');
var bodyParser = require('body-parser');
var app = express();

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.get('/', function(req, res){
    return http.get({
        host: 'localhost',
        path: '/json'
    }, function(response) {
        // Continuously update stream with data
        var body = '';
        response.on('data', function(d) {
            body += d;
        });
        response.on('end', function() {
            // Data reception is done, do whatever with it!
            var parsed = JSON.parse(body);
            console.log(parsed);
            res.render("index", {result: parsed});
        });
    });

});

app.get('/json', function(req, res){
  var data = [ {
      "length": 123,
      "status": "OPEN"
    },{
    "length": 145,
      "status": "CLOSE"
    }
  ]

  res.json(data);

});

app.listen(80);

module.exports = app;

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

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