如何获得复杂的json对象,并在节点js的视图中呈现它?
我有跟在数组中的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}); }
鉴于,我能够赶上旅行ID,但不pickPointPointDetails和dropoffPointDetails数组内,我怎样才能在视图中呈现?
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 pa(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>