迭代Json数据并在EJS中打印

时间:2018-04-29 19:03:07

标签: javascript json node.js javascript-objects ejs

https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=demo

^^数据引用

这里有一些我想要使用的数据。我制作了自己的Api键并在我的路线中使用它。

var router = require('express').Router();

const stockApi = 'YOUR_API_KEY';


router.get("/", function(req, res) {
  var request = require('request');
    request('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey='+stockApi, function (error, response, body) {
    var json = JSON.parse(body)
      if (error) {
        console.log(error)
      } else {
        console.log(json)
        res.render("home/home", {data:json});
      }
    });

});
module.exports = router;

所以我解析了我的数据并将其传递给response.render作为'数据'。

这是我有点困惑的地方。我正在尝试打印“元数据”#39;和'时间序列'进入ejs模板。但它会抛出错误,因为每个都不是一个函数。我在ejs中使用Json数据有点新鲜。这是我的模板,我只是想打印一个数据。

 <body>
    <%-include('../partials/navbar')%>
    <div class="container">
      <div class="row mt-4">
        <div class="col">
          <% data.forEach((stock) => { %>
            <p><%=stock.Meta_Data %></p>

          <%})%>
        </div>
      </div>
    </div>
  </body>

当我发出请求时,我确实看到了控制台中的所有数据。我似乎无法打印出我想要的物体。

2 个答案:

答案 0 :(得分:1)

您可以使用for...in

执行此类操作

&#13;
&#13;
var obj = {"Meta Data":{"1. Information":"Intraday (1min) prices and volumes","2. Symbol":"MSFT","3. Last Refreshed":"2018-04-30 16:00:00","4. Interval":"1min","5. Output Size":"Compact","6. Time Zone":"US/Eastern"},"Time Series (1min)":{"2018-04-30 16:00:00":{"1. open":"93.6600","2. high":"93.7600","3. low":"93.4800","4. close":"93.5200","5. volume":"6710769"},"2018-04-30 15:59:00":{"1. open":"93.7100","2. high":"93.7100","3. low":"93.6100","4. close":"93.6600","5. volume":"259169"},"2018-04-30 15:58:00":{"1. open":"93.6700","2. high":"93.7100","3. low":"93.6600","4. close":"93.7050","5. volume":"171998"},"2018-04-30 15:57:00":{"1. open":"93.6900","2. high":"93.7100","3. low":"93.6150","4. close":"93.6600","5. volume":"198574"},"2018-04-30 15:56:00":{"1. open":"93.6500","2. high":"93.7300","3. low":"93.6500","4. close":"93.6966","5. volume":"187325"},"2018-04-30 15:55:00":{"1. open":"93.5800","2. high":"93.6600","3. low":"93.5500","4. close":"93.6500","5. volume":"134861"},"2018-04-30 15:54:00":{"1. open":"93.6650","2. high":"93.6900","3. low":"93.5800","4. close":"93.5800","5. volume":"116057"},"2018-04-30 15:53:00":{"1. open":"93.6850","2. high":"93.7400","3. low":"93.6500","4. close":"93.6650","5. volume":"149629"},"2018-04-30 15:52:00":{"1. open":"93.7700","2. high":"93.7800","3. low":"93.6700","4. close":"93.6900","5. volume":"182107"},"2018-04-30 15:51:00":{"1. open":"93.9700","2. high":"94.0100","3. low":"93.7692","4. close":"93.7692","5. volume":"271349"},"2018-04-30 15:50:00":{"1. open":"94.0850","2. high":"94.1185","3. low":"93.8200","4. close":"93.9600","5. volume":"143081"},"2018-04-30 15:49:00":{"1. open":"94.0400","2. high":"94.1000","3. low":"94.0400","4. close":"94.0899","5. volume":"66721"},"2018-04-30 15:48:00":{"1. open":"94.1050","2. high":"94.1100","3. low":"94.0100","4. close":"94.0200","5. volume":"128293"},"2018-04-30 15:47:00":{"1. open":"94.1250","2. high":"94.1700","3. low":"94.1050","4. close":"94.1050","5. volume":"89355"},"2018-04-30 15:46:00":{"1. open":"94.1350","2. high":"94.2900","3. low":"94.1100","4. close":"94.1250","5. volume":"199854"},"2018-04-30 15:45:00":{"1. open":"94.1600","2. high":"94.2200","3. low":"94.1300","4. close":"94.1350","5. volume":"141196"},"2018-04-30 15:44:00":{"1. open":"94.0950","2. high":"94.2000","3. low":"94.0900","4. close":"94.1700","5. volume":"117068"},"2018-04-30 15:43:00":{"1. open":"94.1250","2. high":"94.1500","3. low":"94.0700","4. close":"94.1000","5. volume":"69581"},"2018-04-30 15:42:00":{"1. open":"94.1550","2. high":"94.1600","3. low":"94.0950","4. close":"94.1200","5. volume":"59339"},"2018-04-30 15:41:00":{"1. open":"94.1650","2. high":"94.1800","3. low":"94.1150","4. close":"94.1550","5. volume":"104516"},"2018-04-30 15:40:00":{"1. open":"94.0701","2. high":"94.1800","3. low":"94.0700","4. close":"94.1650","5. volume":"85866"},"2018-04-30 15:39:00":{"1. open":"94.1200","2. high":"94.1281","3. low":"94.0600","4. close":"94.0750","5. volume":"60088"},"2018-04-30 15:38:00":{"1. open":"94.0900","2. high":"94.1400","3. low":"94.0800","4. close":"94.1250","5. volume":"57689"},"2018-04-30 15:37:00":{"1. open":"94.1250","2. high":"94.1400","3. low":"94.0300","4. close":"94.0950","5. volume":"71865"},"2018-04-30 15:36:00":{"1. open":"94.1750","2. high":"94.2100","3. low":"94.1200","4. close":"94.1300","5. volume":"69678"},"2018-04-30 15:35:00":{"1. open":"94.1000","2. high":"94.2300","3. low":"94.0900","4. close":"94.1700","5. volume":"145388"},"2018-04-30 15:34:00":{"1. open":"94.0350","2. high":"94.1000","3. low":"94.0100","4. close":"94.0900","5. volume":"69089"},"2018-04-30 15:33:00":{"1. open":"94.0050","2. high":"94.0600","3. low":"94.0000","4. close":"94.0300","5. volume":"44944"},"2018-04-30 15:32:00":{"1. open":"94.0000","2. high":"94.0618","3. low":"93.9900","4. close":"94.0000","5. volume":"50624"},"2018-04-30 15:31:00":{"1. open":"93.9300","2. high":"94.0000","3. low":"93.9300","4. close":"94.0000","5. volume":"46465"},"2018-04-30 15:30:00":{"1. open":"93.9400","2. high":"93.9900","3. low":"93.9300","4. close":"93.9400","5. volume":"69917"},"2018-04-30 15:29:00":{"1. open":"93.8700","2. high":"93.9300","3. low":"93.8500","4. close":"93.9300","5. volume":"38623"},"2018-04-30 15:28:00":{"1. open":"93.8200","2. high":"93.8900","3. low":"93.8050","4. close":"93.8700","5. volume":"26722"},"2018-04-30 15:27:00":{"1. open":"93.8300","2. high":"93.8500","3. low":"93.7900","4. close":"93.8239","5. volume":"27912"},"2018-04-30 15:26:00":{"1. open":"93.8250","2. high":"93.8500","3. low":"93.8200","4. close":"93.8300","5. volume":"24261"},"2018-04-30 15:25:00":{"1. open":"93.8175","2. high":"93.8461","3. low":"93.7900","4. close":"93.8300","5. volume":"33380"},"2018-04-30 15:24:00":{"1. open":"93.9100","2. high":"93.9500","3. low":"93.7600","4. close":"93.8100","5. volume":"86405"},"2018-04-30 15:23:00":{"1. open":"93.9100","2. high":"93.9200","3. low":"93.8500","4. close":"93.9015","5. volume":"46606"},"2018-04-30 15:22:00":{"1. open":"94.0200","2. high":"94.0800","3. low":"93.8900","4. close":"93.9000","5. volume":"80331"},"2018-04-30 15:21:00":{"1. open":"94.0300","2. high":"94.0300","3. low":"93.9900","4. close":"94.0199","5. volume":"21440"},"2018-04-30 15:20:00":{"1. open":"93.9800","2. high":"94.0342","3. low":"93.9650","4. close":"94.0342","5. volume":"20556"},"2018-04-30 15:19:00":{"1. open":"93.9600","2. high":"93.9900","3. low":"93.9500","4. close":"93.9900","5. volume":"19247"},"2018-04-30 15:18:00":{"1. open":"93.9300","2. high":"93.9966","3. low":"93.9300","4. close":"93.9600","5. volume":"20679"},"2018-04-30 15:17:00":{"1. open":"94.0200","2. high":"94.0300","3. low":"93.9200","4. close":"93.9200","5. volume":"65818"},"2018-04-30 15:16:00":{"1. open":"94.0100","2. high":"94.1300","3. low":"93.9950","4. close":"94.0300","5. volume":"139065"},"2018-04-30 15:15:00":{"1. open":"94.0200","2. high":"94.0900","3. low":"94.0000","4. close":"94.0100","5. volume":"30109"},"2018-04-30 15:14:00":{"1. open":"94.0300","2. high":"94.0500","3. low":"94.0050","4. close":"94.0300","5. volume":"18015"},"2018-04-30 15:13:00":{"1. open":"94.0100","2. high":"94.0600","3. low":"93.9900","4. close":"94.0300","5. volume":"40470"},"2018-04-30 15:12:00":{"1. open":"93.9750","2. high":"94.0200","3. low":"93.9400","4. close":"94.0061","5. volume":"36967"},"2018-04-30 15:11:00":{"1. open":"93.9250","2. high":"94.0200","3. low":"93.9201","4. close":"93.9800","5. volume":"39671"},"2018-04-30 15:10:00":{"1. open":"93.9100","2. high":"93.9800","3. low":"93.9050","4. close":"93.9300","5. volume":"28209"},"2018-04-30 15:09:00":{"1. open":"93.9000","2. high":"94.0200","3. low":"93.9000","4. close":"93.9150","5. volume":"37855"},"2018-04-30 15:08:00":{"1. open":"93.9150","2. high":"93.9400","3. low":"93.8800","4. close":"93.9000","5. volume":"32684"},"2018-04-30 15:07:00":{"1. open":"93.8700","2. high":"93.9700","3. low":"93.8700","4. close":"93.9200","5. volume":"44537"},"2018-04-30 15:06:00":{"1. open":"93.7900","2. high":"93.8700","3. low":"93.7600","4. close":"93.8650","5. volume":"29812"},"2018-04-30 15:05:00":{"1. open":"93.8300","2. high":"93.8700","3. low":"93.7700","4. close":"93.7900","5. volume":"49564"},"2018-04-30 15:04:00":{"1. open":"93.7900","2. high":"93.8400","3. low":"93.7800","4. close":"93.8300","5. volume":"38744"},"2018-04-30 15:03:00":{"1. open":"93.8700","2. high":"93.8900","3. low":"93.7800","4. close":"93.7800","5. volume":"41179"},"2018-04-30 15:02:00":{"1. open":"93.8500","2. high":"93.9000","3. low":"93.8300","4. close":"93.8700","5. volume":"37526"},"2018-04-30 15:01:00":{"1. open":"93.8300","2. high":"93.8900","3. low":"93.7900","4. close":"93.8501","5. volume":"31866"},"2018-04-30 15:00:00":{"1. open":"93.8800","2. high":"93.9200","3. low":"93.8250","4. close":"93.8250","5. volume":"36569"},"2018-04-30 14:59:00":{"1. open":"93.8900","2. high":"93.9200","3. low":"93.8800","4. close":"93.8800","5. volume":"16199"},"2018-04-30 14:58:00":{"1. open":"93.9700","2. high":"93.9700","3. low":"93.8700","4. close":"93.8850","5. volume":"60618"},"2018-04-30 14:57:00":{"1. open":"94.0100","2. high":"94.0400","3. low":"93.9600","4. close":"93.9700","5. volume":"16716"},"2018-04-30 14:56:00":{"1. open":"94.0099","2. high":"94.0700","3. low":"93.9700","4. close":"94.0000","5. volume":"32260"},"2018-04-30 14:55:00":{"1. open":"93.9900","2. high":"94.0900","3. low":"93.9700","4. close":"94.0099","5. volume":"38314"},"2018-04-30 14:54:00":{"1. open":"94.0600","2. high":"94.0600","3. low":"93.9700","4. close":"93.9900","5. volume":"37277"},"2018-04-30 14:53:00":{"1. open":"94.0700","2. high":"94.0800","3. low":"93.9500","4. close":"94.0600","5. volume":"48356"},"2018-04-30 14:52:00":{"1. open":"94.0108","2. high":"94.0790","3. low":"93.9850","4. close":"94.0750","5. volume":"27780"},"2018-04-30 14:51:00":{"1. open":"94.0100","2. high":"94.0600","3. low":"94.0000","4. close":"94.0200","5. volume":"23033"},"2018-04-30 14:50:00":{"1. open":"93.9700","2. high":"94.0200","3. low":"93.9400","4. close":"94.0020","5. volume":"18227"},"2018-04-30 14:49:00":{"1. open":"93.8900","2. high":"93.9800","3. low":"93.8850","4. close":"93.9800","5. volume":"22587"},"2018-04-30 14:48:00":{"1. open":"93.8800","2. high":"93.9199","3. low":"93.8410","4. close":"93.8900","5. volume":"46588"},"2018-04-30 14:47:00":{"1. open":"93.9150","2. high":"93.9300","3. low":"93.8200","4. close":"93.8800","5. volume":"40456"},"2018-04-30 14:46:00":{"1. open":"93.9400","2. high":"93.9800","3. low":"93.9100","4. close":"93.9100","5. volume":"15290"},"2018-04-30 14:45:00":{"1. open":"93.9250","2. high":"93.9700","3. low":"93.9250","4. close":"93.9439","5. volume":"12610"},"2018-04-30 14:44:00":{"1. open":"94.0000","2. high":"94.0000","3. low":"93.8700","4. close":"93.9234","5. volume":"42916"},"2018-04-30 14:43:00":{"1. open":"93.9550","2. high":"94.0400","3. low":"93.9500","4. close":"94.0010","5. volume":"64156"},"2018-04-30 14:42:00":{"1. open":"94.0150","2. high":"94.0400","3. low":"93.9400","4. close":"93.9500","5. volume":"36427"},"2018-04-30 14:41:00":{"1. open":"94.0150","2. high":"94.0500","3. low":"94.0050","4. close":"94.0200","5. volume":"21677"},"2018-04-30 14:40:00":{"1. open":"94.0750","2. high":"94.0800","3. low":"93.9900","4. close":"94.0100","5. volume":"45532"},"2018-04-30 14:39:00":{"1. open":"94.0400","2. high":"94.1700","3. low":"94.0400","4. close":"94.0700","5. volume":"44581"},"2018-04-30 14:38:00":{"1. open":"94.1000","2. high":"94.1100","3. low":"94.0200","4. close":"94.0350","5. volume":"30249"},"2018-04-30 14:37:00":{"1. open":"94.0700","2. high":"94.1150","3. low":"94.0400","4. close":"94.1050","5. volume":"40627"},"2018-04-30 14:36:00":{"1. open":"94.0800","2. high":"94.1200","3. low":"94.0400","4. close":"94.0700","5. volume":"25601"},"2018-04-30 14:35:00":{"1. open":"94.0200","2. high":"94.1250","3. low":"94.0200","4. close":"94.0900","5. volume":"45214"},"2018-04-30 14:34:00":{"1. open":"94.1200","2. high":"94.1900","3. low":"94.0000","4. close":"94.0100","5. volume":"74102"},"2018-04-30 14:33:00":{"1. open":"94.0316","2. high":"94.1200","3. low":"94.0200","4. close":"94.1200","5. volume":"32945"},"2018-04-30 14:32:00":{"1. open":"94.0850","2. high":"94.1400","3. low":"94.0200","4. close":"94.0350","5. volume":"29269"},"2018-04-30 14:31:00":{"1. open":"94.1300","2. high":"94.1450","3. low":"94.0800","4. close":"94.0800","5. volume":"32486"},"2018-04-30 14:30:00":{"1. open":"94.1200","2. high":"94.2300","3. low":"94.1150","4. close":"94.1400","5. volume":"46830"},"2018-04-30 14:29:00":{"1. open":"94.0800","2. high":"94.1500","3. low":"94.0750","4. close":"94.1100","5. volume":"39559"},"2018-04-30 14:28:00":{"1. open":"94.1450","2. high":"94.1600","3. low":"94.0600","4. close":"94.0700","5. volume":"30840"},"2018-04-30 14:27:00":{"1. open":"94.1200","2. high":"94.1750","3. low":"94.1050","4. close":"94.1400","5. volume":"36854"},"2018-04-30 14:26:00":{"1. open":"94.1100","2. high":"94.1900","3. low":"94.1050","4. close":"94.1250","5. volume":"27821"},"2018-04-30 14:25:00":{"1. open":"94.1350","2. high":"94.2200","3. low":"94.1000","4. close":"94.1200","5. volume":"37538"},"2018-04-30 14:24:00":{"1. open":"94.1550","2. high":"94.2100","3. low":"94.1250","4. close":"94.1300","5. volume":"36620"},"2018-04-30 14:23:00":{"1. open":"94.1900","2. high":"94.2450","3. low":"94.1400","4. close":"94.1550","5. volume":"38887"},"2018-04-30 14:22:00":{"1. open":"94.1000","2. high":"94.2200","3. low":"94.0700","4. close":"94.1900","5. volume":"61078"},"2018-04-30 14:21:00":{"1. open":"94.0000","2. high":"94.1270","3. low":"93.9939","4. close":"94.1000","5. volume":"41664"}}};

// This is for Meta Data
for (var meta_data in obj["Meta Data"]){
  console.log(meta_data);
}

// This is for Time Series Data
for (var time_series in obj["Time Series (1min)"]){
    console.log(time_series);
  for(var time_series_data in obj["Time Series (1min)"][time_series]){
    console.log(time_series_data);
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

请安装Alpha Vantage Wrapper的npm软件包

1。npm install --save alpha_vantage_api_wrapper for NPM

const express = require('express');
const app =express();

var Alpha = require('alpha_vantage_api_wrapper').Alpha;                       //Alpha Wrapper
var alpha = new Alpha('demo');  
var stock_Dates = [];                                                         //Array Of Data      
var stock_opens = [];
var stock_highs = [];
var stock_lows = [];
var stock_closes = [];
var stock_volumes = [];

app.get('/', function(req,res){
    res.render('list', { query, stock_Dates, stock_opens, stock_highs, stock_lows, stock_closes, stock_volumes });
                                                                            //Injected in to views
});

app.post('/', function(req,res) {
        const query = req.body.stockName;
        alpha.stocks.intraday(query)                                        //Alpha Api-wrapper Function
            .then((data) => {
                const intraDay = (data['Time Series (5min)']);              

                for(var update in intraDay){
                    var stock_Date = update;
                    var stock_open = intraDay[update]['1. open'];           //Narrowing the endpoints for accessing the req Data
                    var stock_high = intraDay[update]['2. high'];
                    var stock_low = intraDay[update]['3. low'];
                    var stock_close = intraDay[update]['4. close'];
                    var stock_volume = intraDay[update]['5. volume'];
                    stock_Dates.push(stock_Date);                           //Pushing the Data into the Array of Data
                    stock_opens.push(stock_open);       
                    stock_highs.push(stock_high);
                    stock_lows.push(stock_low);
                    stock_closes.push(stock_close);
                    stock_volumes.push(stock_volume);
                }
                res.redirect('/');                                          //Redirects To the hamepage
            })
            .catch((err) => {
                // Handle the error
                console.log(err);                                           //Logs error if any
            });
        
});