javascript - 如何获取复杂的json对象并将其呈现在 Node js的 View 中?

标签 javascript json node.js pug

我有以下 json,其中包含数组

在服务器中我发送这样的 json

getTrips: function getTrips(req, res, next){
     var url = '/CTB-WS/rest/trips?from='+ req.tripinfo.fromCityId + '&to=' + req.tripinfo.toCityId + '&depart-date=' + req.tripinfo.departDate+ '&pax=1';
     console.log(url);
     rest.get(url).on('complete', function(trips) {
      if (trips instanceof Error) {
        console.log('Error:', trips.message);
        } else {        
          console.log('trips'+ JSON.stringify(trips)); 
          console.log('onward trips'+ JSON.stringify(trips['onwardTrips']));  
          trips = trips || [];
          req.trips = trips['onwardTrips'];
          next();
        }
    });
  },

  sendTrips: function sendTrips(req, res, next){
   res.render('trips', { trips: req.trips});

  }

在 View 中,我能够捕获行程 id,但不能捕获数组内的pickupPointDetails 和 dropoffPointDetails,如何在 View 中渲染它?

extends layout

block content
  h3 Trip Selection
    form.form-horizontal(id="Findtrips", accept-charset="UTF-8", action="", method="post" enctype="multipart/form-data")
      each trip, key in trips
    p 
      a(href="") #{trip.tripId} #{key} #{trips.length}
    p= trip.pickupPointDetails[0].pickupPointId  //[0] [1] works but when i give key as value Unexpected token =

JSON 对象

{
  "onwardTrips": [
    {
      "tripId": "1285758",
      "fromCity": "Singapore",
      "toCity": "Shah Alam",
      "operatorCode": "SA",
      "operatorName": "Starmart Express",
      "departTime": "2014-01-24 11:30:00.0",
      "busType": "Executive",
      "pickupPointDetails": [
        {
          "pickupPointId": "78",
          "departureTime": "2014-01-24 11:30:00.0",
          "pickupPointName": "Golden Mile Tower, Beach Road"
        }
      ],
      "dropoffPointDetails": [
        {
          "dropOffPointName": "Shah Alam Bus Terminal",
          "dropOffPointId": "1285758"
        }
      ],
      "fareDetails": {
        "adultFare": "91.0"
      }
    },
    {
      "tripId": "1285856",
      "fromCity": "Singapore",
      "toCity": "Shah Alam",
      "operatorCode": "SA",
      "operatorName": "Starmart Express",
      "departTime": "2014-01-24 21:00:00.0",
      "busType": "Executive",
      "pickupPointDetails": [
        {
          "pickupPointId": "78",
          "departureTime": "2014-01-24 21:00:00.0",
          "pickupPointName": "Golden Mile Tower, Beach Road"
        }
      ],
      "dropoffPointDetails": [
        {
          "dropOffPointName": "Shah Alam Bus Terminal",
          "dropOffPointId": "1285856"
        }
      ],
      "fareDetails": {
        "adultFare": "91.0"
      }
    }
  ],
  "errorCode": 0
}

最佳答案

如果您希望访问子数组中的每个对象,则需要嵌套另一个循环:

JSON 对象示例:

{
  "array": [
    {
      "property": "Hello",
      "nestedArray": [
         {
           "nestedArrayProp": "World"
         }
      ]
    }
  ]
}

Jade 模板示例:

each object in array
  each nestedObject in object.nestedArray
    p= object.property + ' ' + nestedObject.nestedArrayProp

将输出:

<p>Hello World</p>

关于javascript - 如何获取复杂的json对象并将其呈现在 Node js的 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21239251/

相关文章:

javascript - 在 angular2 中导出 xlsx 文件( typescript )

javascript - 我正在尝试 .Find() Div 中的 TextArea

json - Marshal/Unmarshal int 类型

java - 无法识别的字段类未标记为可忽略

sql-server - nodejs 出现 mssql 连接超时错误

node.js - Jade Include 不渲染包含的页面内容

javascript - 正则表达式帮助 Angular 过滤器突出显示 html 中的匹配文本

json - 获取 json 动态键名作为字符串?

node.js - 目前最新最好的 socket.io 基准测试模块是什么?

javascript - d3折线图显示不正确