本文介绍了如何获取复杂的json对象并将其呈现在节点js的视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有数组的json

I have following json with arrays in it

在服务器中,我像这样发送json

In server I'm sending json like this

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,如何在视图中呈现它?

In view , I'm able to catch trip id, but not pickupPointDetails and dropoffPointDetails which are inside array, how can I render it in 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
}

推荐答案

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

You need to nest another loop if you wish to access each object in the sub array:

示例JSON对象:

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

示例玉模板:

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

哪个会输出:

<p>Hello World</p>

这篇关于如何获取复杂的json对象并将其呈现在节点js的视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:03