从Nodejs服务器将JSON数组数据解析为Jade模板

时间:2019-05-15 16:31:20

标签: node.js express pug

我正在尝试遍历并在我的Jade模板上显示我的JSON对象数组数据。 我在想这可能是我的JSON对象,但不确定。 我尝试进行字符串化处理,而不是在“渲染”命令上进行处理。

我已经尝试过使用玉器的每个命令以及此处给出的其他所有答案,以查找与我要寻找的东西相近的东西。我认为我与JADE的工作方式脱节。

dailyforecastdata = {
    days: []
  };


for (var key in forecastdata) {    //look through data at the period level 
  nameoutput = forecastdata[key].name
  numberoutput = forecastdata[key].number
  starttimeoutput = forecastdata[key].startTime
  endtimeoutput = forecastdata[key].endTime
  isdaytimeoutput = forecastdata[key].isDaytime
  temperatureoutput = forecastdata[key].temperature
  temperatureunitoutput = forecastdata[key].temperatureUnit
  temperatureTrend = forecastdata[key].temperatureTrend
  windSpeed = forecastdata[key].windSpeed
  windDirection = forecastdata[key].windDirection
  icon = forecastdata[key].icon
  shortForecast = forecastdata[key].shortForecast
  detailedForecast =  forecastdata[key].detailedForecast

dailyforecastdata.days.push({
'name' : nameoutput,
'number' : numberoutput,
'starttime' : starttimeoutput,
'endtime' : endtimeoutput,
'isday' : isdaytimeoutput,
'temp' : temperatureoutput, 
'tempunit': temperatureunitoutput,
'temptrend' : temperatureTrend, 
'windSpeed' : windSpeed, 
'windDirection' : windDirection, 
'icon' : icon, 
'shortForecast' : shortForecast, 
'detailedForecast' : detailedForecast
});


 res.render("nws", {forecastdata: JSON.stringify(dailyforecastdata)});
 //res.render("nws", {forecastdata: dailyforecastdata});



.JADE FILE
I knowp #{forecastdata} isn't what i need but it's the only thing i've gotten to work in any way of displaying my data. 

extends layout

block content
    h1 Weather Forecast
    p #{forecastdata}

#{forecastdata}的输出

{"days":[{"name":"Today","number":1,"starttime":"2019-05-15T11:00:00-05:00","endtime":"2019-05-15T18:00:00-05:00","isday":true,"temp":86,"tempunit":"F","temptrend":null,"windSpeed":"10 mph","windDirection":"SE","icon":"https://api.weather.gov/icons/land/day/tsra_hi,20/sct?size=medium","shortForecast":"Slight Chance Showers And Thunderstorms then Mostly Sunny","detailedForecast":"A slight chance of showers and thunderstorms before noon. Mostly sunny, with a high near 86. Southeast wind around 10 mph, with gusts as high as 20 mph. Chance of precipitation is 20%."},{"name":"Tonight","number":2,"starttime":"2019-05-15T18:00:00-05:00","endtime":"2019-05-16T06:00:00-05:00","isday":false,"temp":64,"tempunit":"F","temptrend":null,"windSpeed":"10 mph","windDirection":"S","icon":"https://api.weather.gov/icons/land/night/few?size=medium","shortForecast":"Mostly Clear","detailedForecast":"Mostly clear, with a low around 64. South wind around 10 mph."},{"name":"Thursday","number":3,"starttime":"2019-05-16T06:00:00-05:00","endtime":"2019-05-16T18:00:00-05:00","isday":true,"temp":92,"tempunit":"F","temptrend":null,"windSpeed":"10 to 20 mph","windDirection":"SW","icon":"https://api.weather.gov/icons/land/day/few?size=medium","shortForecast":"Sunny","detailedForecast":"Sunny, with a high near 92. Southwest wind 10 to 20 mph, with gusts as high as 35 mph."},{"name":"Thursday Night","number":4,"starttime":"2019-05-16T18:00:00-05:00","endtime":"2019-05-17T06:00:00-05:00","isday":false,"temp":68,"tempunit":"F","temptrend":null,"windSpeed":"15 mph","windDirection":"S","icon":"https://api.weather.gov/icons/land/night/few?size=medium","shortForecast":"Mostly Clear","detailedForecast":"Mostly clear, with a low around 68. South wind around 15 mph, with gusts as high as 30 mph."},{"name":"Friday","number":5,"starttime":"2019-05-17T06:00:00-05:00","endtime":"2019-05-17T18:00:00-05:00","isday":true,"temp":86,"tempunit":"F","temptrend":null,"windSpeed":"15 to 20 mph","windDirection":"S","icon":"https://api.weather.gov/icons/land/day/sct?size=medium","shortForecast":"Mostly Sunny","detailedForecast":"Mostly sunny, with a high near 86. South wind 15 to 20 mph, with gusts as high as 35 mph."},{"name":"Friday Night","number":6,"starttime":"2019-05-17T18:00:00-05:00","endtime":"2019-05-18T06:00:00-05:00","isday":false,"temp":68,"tempunit":"F","temptrend":null,"windSpeed":"15 to 20 mph","windDirection":"S","icon":"https://api.weather.gov/icons/land/night/bkn/tsra_sct,20?size=medium","shortForecast":"Mostly Cloudy then Slight Chance Showers And Thunderstorms","detailedForecast":"A slight chance of showers and thunderstorms after 1am. Mostly cloudy, with a low around 68. Chance of precipitation is 20%."},{"name":"Saturday","number":7,"starttime":"2019-05-18T06:00:00-05:00","endtime":"2019-05-18T18:00:00-05:00","isday":true,"temp":78,"tempunit":"F","temptrend":null,"windSpeed":"15 mph","windDirection":"S","icon":"https://api.weather.gov/icons/land/day/tsra,20/tsra,80?size=medium","shortForecast":"Slight Chance Showers And Thunderstorms","detailedForecast":"A slight chance of showers and thunderstorms before 1pm, then showers and thunderstorms. Mostly cloudy, with a high near 78. Chance of precipitation is 80%."},{"name":"Saturday Night","number":8,"starttime":"2019-05-18T18:00:00-05:00","endtime":"2019-05-19T06:00:00-05:00","isday":false,"temp":55,"tempunit":"F","temptrend":null,"windSpeed":"5 to 10 mph","windDirection":"S","icon":"https://api.weather.gov/icons/land/night/tsra,80/tsra,60?size=medium","shortForecast":"Showers And Thunderstorms","detailedForecast":"Showers and thunderstorms. Mostly cloudy, with a low around 55. Chance of precipitation is 80%."},{"name":"Sunday","number":9,"starttime":"2019-05-19T06:00:00-05:00","endtime":"2019-05-19T18:00:00-05:00","isday":true,"temp":68,"tempunit":"F","temptrend":null,"windSpeed":"10 to 15 mph","windDirection":"W","icon":"https://api.weather.gov/icons/land/day/tsra_sct,60/tsra_sct,30?size=medium","shortForecast":"Showers And Thunderstorms Likely","detailedForecast":"Showers and thunderstorms likely before 1pm, then a slight chance of showers and thunderstorms. Partly sunny, with a high near 68. Chance of precipitation is 60%."},{"name":"Sunday Night","number":10,"starttime":"2019-05-19T18:00:00-05:00","endtime":"2019-05-20T06:00:00-05:00","isday":false,"temp":48,"tempunit":"F","temptrend":null,"windSpeed":"5 to 15 mph","windDirection":"NW","icon":"https://api.weather.gov/icons/land/night/tsra_hi,20/sct?size=medium","shortForecast":"Slight Chance Showers And Thunderstorms then Partly Cloudy","detailedForecast":"A slight chance of showers and thunderstorms before 7pm. Partly cloudy, with a low around 48. Chance of precipitation is 20%."},{"name":"Monday","number":11,"starttime":"2019-05-20T06:00:00-05:00","endtime":"2019-05-20T18:00:00-05:00","isday":true,"temp":70,"tempunit":"F","temptrend":null,"windSpeed":"10 mph","windDirection":"E","icon":"https://api.weather.gov/icons/land/day/tsra_hi,20?size=medium","shortForecast":"Slight Chance Showers And Thunderstorms","detailedForecast":"A slight chance of showers and thunderstorms after 7am. Partly sunny, with a high near 70. Chance of precipitation is 20%."},{"name":"Monday Night","number":12,"starttime":"2019-05-20T18:00:00-05:00","endtime":"2019-05-21T06:00:00-05:00","isday":false,"temp":55,"tempunit":"F","temptrend":null,"windSpeed":"10 mph","windDirection":"E","icon":"https://api.weather.gov/icons/land/night/tsra,50?size=medium","shortForecast":"Slight Chance Showers And Thunderstorms then Chance Showers And Thunderstorms","detailedForecast":"A slight chance of showers and thunderstorms before 7pm, then a chance of showers and thunderstorms. Mostly cloudy, with a low around 55. Chance of precipitation is 50%."},{"name":"Tuesday","number":13,"starttime":"2019-05-21T06:00:00-05:00","endtime":"2019-05-21T18:00:00-05:00","isday":true,"temp":72,"tempunit":"F","temptrend":null,"windSpeed":"10 to 15 mph","windDirection":"SE","icon":"https://api.weather.gov/icons/land/day/tsra,60?size=medium","shortForecast":"Chance Showers And Thunderstorms then Showers And Thunderstorms Likely","detailedForecast":"A chance of showers and thunderstorms before 7am, then showers and thunderstorms likely. Mostly cloudy, with a high near 72. Chance of precipitation is 60%."},{"name":"Tuesday Night","number":14,"starttime":"2019-05-21T18:00:00-05:00","endtime":"2019-05-22T06:00:00-05:00","isday":false,"temp":55,"tempunit":"F","temptrend":null,"windSpeed":"10 to 15 mph","windDirection":"S","icon":"https://api.weather.gov/icons/land/night/tsra_sct,60/tsra_sct,40?size=medium","shortForecast":"Showers And Thunderstorms Likely then Chance Showers And Thunderstorms","detailedForecast":"Showers and thunderstorms likely before 7pm, then a chance of showers and thunderstorms between 7pm and 1am. Mostly cloudy, with a low around 55. Chance of precipitation is 60%."}]}

1 个答案:

答案 0 :(得分:0)

如果#{forecastdata}正确地输出了JSON,则以下each语法应适用:

each day in forecastdata.days
    article
        h2 #{day.name}