【发布时间】:2017-08-07 07:14:57
【问题描述】:
我正在处理一个学校项目,并试图从 URL 获取数据以与我的 Alexa Skill 集成。我绝对是 NodeJS 初学者,也没有太多的 HTML 或 JSON 背景。
我的学校在这里为我们的交通系统提供了一个“API”:https://prtstatus.wvu.edu/api/ 带有时间戳的示例案例在这里:https://prtstatus.wvu.edu/api/1501906657/?format=json
在我的代码中,我试图将来自 URL 的字符串解析为 JSON,但在格式化它时遇到问题,以便传递字符串的“消息:”部分。这是我在 AWS Lambda 中的意图代码:
'getPRTStatus': function() {
var date = Math.round(new Date().getTime()/1000);
var http = require('http');
var https = require('https');
var options = {
host: 'https://prtstatus.wvu.edu',
path: '/api/'+date+'/?format=json'
};
var object;
var callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log(str);
object = JSON.parse(str);
});
}
https.request(options, callback).end();
this.attributes.speechOutput = this.t(object.message);
this.attributes.repromptSpeech = this.t(object.message);
this.emit(':ask', this.attributes.speechOutput, this.attributes.repromptSpeech);
},
【问题讨论】:
标签: javascript json node.js amazon-web-services alexa-skills-kit