【问题标题】:Defining JSON in node.js在 node.js 中定义 JSON
【发布时间】:2016-08-17 02:09:43
【问题描述】:

所以我正在尝试在 node.js 中制作 Alexa 技能 - 但是,我似乎无法弄清楚如何定义 json 元素。我需要加入所有元素,在这种情况下,它们是来自新闻 API 的标题。我将它们全部通过 console.logg'ed 并且它可以工作,但我需要做的就是弄清楚如何使“标题”成为变量。我如何使“标题”成为一个变量以包含 JSON 文件中的所有标题。这是我的代码:

 var Alexa = require('alexa-sdk');
 var request = require('request');

 var APP_ID = "amzn1.ask.skill.36267067-d40c-460c-b07b-cc603b97be1b";
 var url = "https://newsapi.org/v1/articles?source=googlenews&sortBy=top&apiKey=6e23e1ddb67e40cb93cf147718f18e36";


 var handlers = {
     'LaunchRequest': function () {
         this.emit('NewsIntent');
     },

     // Get titles from JSON URL & Output it
     'NewsIntent': function () {

       request({
           url: url,
           json: true
       }, function (error, response, body) {

           if (!error && response.statusCode === 200) {
             console.log(body.articles[0].title);
             console.log(body.articles[1].title);
             console.log(body.articles[2].title);
             console.log(body.articles[3].title);
             console.log(body.articles[4].title);
             console.log(body.articles[5].title);
             console.log(body.articles[6].title);
             console.log(body.articles[7].title);
             console.log(body.articles[8].title);
             console.log(body.articles[9].title);

///// I need help here!!!!! ----> 
       /// need to define title, so I can speech emit it below. 

             this.emit(':tellWithCard', title.join(''));

           }
       });

     }
 };


 exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.APP_ID = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

【问题讨论】:

    标签: javascript json node.js amazon alexa-skills-kit


    【解决方案1】:

    使用map 遍历articles 数组,然后它们可以稍后加入。

    var titles = body.articles.map(function(article) {
      return article.title;
    });
    

    注意:如果有任何未定义的标题将显示在连接中。

    更新:根据您评论中的要点,您可以执行以下操作:

    var handlers = {
      'LaunchRequest': function() {
        this.emit('NewsIntent');
      },
    
      // Get titles from JSON URL & Output it
      'NewsIntent': function() {
    
        request({
          url: url,
          json: true
        }, function(error, response, body) {
          var titles;
          if (!error && response.statusCode === 200) {
            console.log(body.articles[0].title);
            console.log(body.articles[1].title);
            console.log(body.articles[2].title);
            console.log(body.articles[3].title);
            console.log(body.articles[4].title);
            console.log(body.articles[5].title);
            console.log(body.articles[6].title);
            console.log(body.articles[7].title);
            console.log(body.articles[8].title);
            console.log(body.articles[9].title);
    
            titles = body.articles.map(function(article) {
              return article.title;
            });
    
            this.emit(':tellWithCard', titles.join(''));
          }
        });
    
      }
    };
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2014-08-27
    • 2018-10-17
    • 2014-01-24
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    相关资源
    最近更新 更多