代码之家  ›  专栏  ›  技术社区  ›  xTMx

如何从youtube api获取值?

  •  0
  • xTMx  · 技术社区  · 7 年前

    我正在构建一个聊天机器人,根据用户的请求检索youtube链接。 我对bot使用Dialogflow,对代码使用nodejs。

    使用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] } } ]
    

    search(txt1, opts, function(err, results) {
    //var data1 = JSON.parse(results);
    //srchRes = data1.link;
    if(err) return console.log(err);
    
    
    console.dir(results);
    });
    

    SyntaxError: Unexpected token u in JSON at position 0
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   onepix    7 年前

    我们没有你的API调用代码逻辑所以从这里开始, 我们只能假设 results 结果 是一个对象数组。

    link 特别 对象 数组,这就是为什么 JSON.parse(results) 毫无意义。

    链接 每个对象的属性,只需在对象数组上迭代 结果

    for (const result of results) {
        console.log(`the link : ${result.link}`)
    }