【发布时间】:2019-02-28 21:45:00
【问题描述】:
我正在构建一个聊天机器人,它可以根据用户的要求检索 youtube 链接。 我将 Dialogflow 用于机器人并使用 nodejs 进行编码。
现在,一切正常,但我的问题是从 API 响应中检索链接。 使用 console.dir(results),显示如下: 响应如下所示:
[ { id: 'gWNUg_v25dw',
link: 'https://www.youtube.com/watch?v=gWNUg_v25dw',
kind: 'youtube#video',
publishedAt: '2017-08-24T14:00:11.000Z',
channelId: 'UCDZ56yQ05d_ikcwcTG9bAiA',
channelTitle: 'Code to Create',
title: 'How to make a Chatbot with Dialogflow - API.ai',
description: 'In this video, Google Developer Expert, Faisal Abid will show us how to create a chatbot with Google\'s latest API.ai API.ai can be integrated with multiple chat ...',
thumbnails: { default: [Object], medium: [Object], high: [Object] } } ]
我尝试使用 results.link 来检索链接,但我没有定义,所以我的问题是如何从响应块中检索链接?
search(txt1, opts, function(err, results) {
//var data1 = JSON.parse(results);
//srchRes = data1.link;
if(err) return console.log(err);
console.dir(results);
});
取消注释上面的 2 行,console.dir(srchRes) 返回此错误:
SyntaxError: Unexpected token u in JSON at position 0
【问题讨论】:
标签: javascript node.js youtube-api dialogflow-es