首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从节点中的request.body获取值

从节点中的request.body获取值
EN

Stack Overflow用户
提问于 2021-02-15 15:25:33
回答 1查看 66关注 0票数 1

我正在做一个使用谷歌搜索api的节点项目。我通过以下方式得到回应:

代码语言:javascript
复制
{
  kind: 'customsearch#search',
  url: {
    type: 'application/json',
    template: 'https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json'
  },
  queries: { request: [ [Object] ] },
  context: { title: 'CS Curriculum', facets: [ [Array], [Array], [Array] ] },
  searchInformation: {
    searchTime: 0.577799,
    formattedSearchTime: '0.58',
    totalResults: '10',
    formattedTotalResults: '10'
  },
  items: [
    {
      kind: 'customsearch#result',
      title: '2021 News | Department of Computer Science',
      htmlTitle: '2021 <b>News</b> | Department of Computer Science',
      link: 'https://www.cs.utexas.edu/news/articles/index2002',
      displayLink: 'www.cs.utexas.edu',
      snippet: 'Campus health and safety are our top priorities. Get the latest from UT on COVID-\n' +
        '19. Get help with Instructional Continuity and working from home. About · News ...',
      htmlSnippet: 'Campus health and safety are our top priorities. Get the latest from UT on <b>COVID</b>-<br>\n' +
        '19. Get help with Instructional Continuity and working from home. About &middot; <b>News</b>&nbsp;...',
      cacheId: '7AHQ45k8IUUJ',
      formattedUrl: 'https://www.cs.utexas.edu/news/articles/index2002',
      htmlFormattedUrl: 'https://www.cs.utexas.edu/<b>news</b>/<b>articles</b>/index2002',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'New York Daily News | News Sources | Department of Computer ...',
      htmlTitle: 'New York Daily <b>News</b> | <b>News</b> Sources | Department of Computer ...',
      link: 'https://www.cs.columbia.edu/news-source/new-york-daily-news/',
      displayLink: 'www.cs.columbia.edu',
      snippet: 'Learn more about the COVID-19 Response ... In the News ... New Twitter Feature \n' +
        'Challenges Users to Read Articles Before Sharing (Peter Allen,Daniel Bauer ...',
      htmlSnippet: 'Learn more about the <b>COVID</b>-19 Response ... In the <b>News</b> ... New Twitter Feature <br>\n' +
        'Challenges Users to Read <b>Articles</b> Before Sharing (Peter Allen,Daniel Bauer&nbsp;...',
      cacheId: 'H0RrwEK65C8J',
      formattedUrl: 'https://www.cs.columbia.edu/news-source/new-york-daily-news/',
      htmlFormattedUrl: 'https://www.cs.columbia.edu/<b>news</b>-source/new-york-daily-<b>news</b>/',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Smithsonian.com | News Sources | Department of Computer ...',
      htmlTitle: 'Smithsonian.com | <b>News</b> Sources | Department of Computer ...',
      link: 'https://www.cs.columbia.edu/news-source/smithsonian-com/',
      displayLink: 'www.cs.columbia.edu',
      snippet: 'COVID-19 Response. The Columbia Engineering community has come together \n' +
        'to combat the coronavirus pandemic on multiple fronts. In close collabo-ration ...',
      htmlSnippet: '<b>COVID</b>-19 Response. The Columbia Engineering community has come together <br>\n' +
        'to combat the <b>coronavirus</b> pandemic on multiple fronts. In close collabo-ration&nbsp;...',
      cacheId: 'eLnifJe1zycJ',
      formattedUrl: 'https://www.cs.columbia.edu/news-source/smithsonian-com/',
      htmlFormattedUrl: 'https://www.cs.columbia.edu/<b>news</b>-source/smithsonian-com/',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: '2020 News | Page 2 | Department of Computer Science',
      htmlTitle: '2020 <b>News</b> | Page 2 | Department of Computer Science',
      link: 'https://www.cs.utexas.edu/news/articles/index2002?page=1',
      displayLink: 'www.cs.utexas.edu',
      snippet: 'Campus health and safety are our top priorities. Get the latest from UT on COVID-\n' +
        '19. Get help with Instructional Continuity and working from home.',
      htmlSnippet: 'Campus health and safety are our top priorities. Get the latest from UT on <b>COVID</b>-<br>\n' +
        '19. Get help with Instructional Continuity and working from home.',
      cacheId: 'S47VULaZRu8J',
      formattedUrl: 'https://www.cs.utexas.edu/news/articles/index2002?page=1',
      htmlFormattedUrl: 'https://www.cs.utexas.edu/<b>news</b>/<b>articles</b>/index2002?page=1',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Your Houston News | News Sources | Department of Computer ...',
      htmlTitle: 'Your Houston <b>News</b> | <b>News</b> Sources | Department of Computer ...',
      link: 'https://www.cs.columbia.edu/news-source/your-houston-news/',
      displayLink: 'www.cs.columbia.edu',
      snippet: 'Learn more about the COVID-19 Response ... In the News ... New Twitter Feature \n' +
        'Challenges Users to Read Articles Before Sharing (Peter Allen,Daniel Bauer ...',
      htmlSnippet: 'Learn more about the <b>COVID</b>-19 Response ... In the <b>News</b> ... New Twitter Feature <br>\n' +
        'Challenges Users to Read <b>Articles</b> Before Sharing (Peter Allen,Daniel Bauer&nbsp;...',
      cacheId: 'JRaK3HT4SeYJ',
      formattedUrl: 'https://www.cs.columbia.edu/news-source/your-houston-news/',
      htmlFormattedUrl: 'https://www.cs.columbia.edu/<b>news</b>-source/your-houston-<b>news</b>/',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Newsday | News Sources | Department of Computer Science ...',
      htmlTitle: 'Newsday | <b>News</b> Sources | Department of Computer Science ...',
      link: 'https://www.cs.columbia.edu/news-source/newsday/',
      displayLink: 'www.cs.columbia.edu',
      snippet: 'Learn more about the COVID-19 Response ... In the News ... New Twitter Feature \n' +
        'Challenges Users to Read Articles Before Sharing (Peter Allen,Daniel Bauer ...',
      htmlSnippet: 'Learn more about the <b>COVID</b>-19 Response ... In the <b>News</b> ... New Twitter Feature <br>\n' +
        'Challenges Users to Read <b>Articles</b> Before Sharing (Peter Allen,Daniel Bauer&nbsp;...',
      cacheId: 'EwzgYOy-S6YJ',
      formattedUrl: 'https://www.cs.columbia.edu/news-source/newsday/',
      htmlFormattedUrl: 'https://www.cs.columbia.edu/<b>news</b>-source/<b>news</b>day/',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Globalization and Offshoring of Software',
      htmlTitle: 'Globalization and Offshoring of Software',
      link: 'https://www.acm.org/binaries/content/assets/public-policy/usacm/intellectual-property/reports-and-white-papers/full_final1.pdf',
      displayLink: 'www.acm.org',
      snippet: 'While articles on offshoring and outsourcing appeared in the media weekly, ... \n' +
        'Source: Compilation from various newspaper articles by Martin Wildemann 2005:\n' +
        ' ...',
      htmlSnippet: 'While <b>articles</b> on offshoring and outsourcing appeared in the media weekly, ... <br>\n' +
        'Source: Compilation from various <b>newspaper articles</b> by Martin Wildemann 2005:<br>\n' +
        '&nbsp;...',
      formattedUrl: 'https://www.acm.org/binaries/content/assets/public.../full_final1.pdf',
      htmlFormattedUrl: 'https://www.acm.org/binaries/content/assets/public.../full_final1.pdf',
      pagemap: [Object],
      mime: 'application/pdf',
      fileFormat: 'PDF/Adobe Acrobat'
    },
    {
      kind: 'customsearch#result',
      title: 'data hub | Department of Computer Science, Columbia University',
      htmlTitle: 'data hub | Department of Computer Science, Columbia University',
      link: 'https://www.cs.columbia.edu/tag/data-hub/',
      displayLink: 'www.cs.columbia.edu',
      snippet: 'For the COVID-19 Hub, CRAC was asked to join a team along with CUIT and \n' +
        'senior leadership across every ... I was interested because of the COVID-related \n' +
        'nature of the project. ... In the News ... New Twitter Feature Challenges Users to \n' +
        'Read Articles Before Sharing (Peter Allen,Daniel Bauer,Augustin Chaintreau) ...',
      htmlSnippet: 'For the <b>COVID</b>-19 Hub, CRAC was asked to join a team along with CUIT and <br>\n' +
        'senior leadership across every ... I was interested because of the <b>COVID</b>-related <br>\n' +
        'nature of the project. ... In the <b>News</b> ... New Twitter Feature Challenges Users to <br>\n' +
        'Read <b>Articles</b> Before Sharing (Peter Allen,Daniel Bauer,Augustin Chaintreau)&nbsp;...',
      cacheId: 'r5LNPkX3GMIJ',
      formattedUrl: 'https://www.cs.columbia.edu/tag/data-hub/',
      htmlFormattedUrl: 'https://www.cs.columbia.edu/tag/data-hub/',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Association for Computing Machinery',
      htmlTitle: 'Association for Computing Machinery',
      link: 'https://www.acm.org/?p=t&offset=45',
      displayLink: 'www.acm.org',
      snippet: 'Read the IFIP news release. ... CRA Surveys Computer Science Faculty, \n' +
        "Department Heads on COVID-19's Impacts ... sensitive interests FB has assigned \n" +
        'them in a Contributed Article in the January 2021 issue of Communications of the \n' +
        'ACM.',
      htmlSnippet: 'Read the IFIP <b>news</b> release. ... CRA Surveys Computer Science Faculty, <br>\n' +
        'Department Heads on <b>COVID</b>-19&#39;s Impacts ... sensitive interests FB has assigned <br>\n' +
        'them in a Contributed <b>Article</b> in the January 2021 issue of Communications of the <br>\n' +
        'ACM.',
      cacheId: '4E0D_zMzG8kJ',
      formattedUrl: 'https://www.acm.org/?p=t&offset=45',
      htmlFormattedUrl: 'https://www.acm.org/?p=t&amp;offset=45',
      pagemap: [Object]
    },
    {
      kind: 'customsearch#result',
      title: 'Revelation and Bible Prophecy',
      htmlTitle: 'Revelation and Bible Prophecy',
      link: 'http://www.cs.ubc.ca/~knorr/public/comparison_of_eschat_models.pdf',
      displayLink: 'www.cs.ubc.ca',
      snippet: '6 days ago ... dispensationalists) have a newspaper in one hand, and a Bible in the other. \n' +
        'Instead ... articles (“a”, “an”, or “the”) present in some of those passages in the \n' +
        'Greek. ... events (e.g., flu outbreaks, COVID-19 coronavirus), corporate.',
      htmlSnippet: '6 days ago <b>...</b> dispensationalists) have a <b>newspaper</b> in one hand, and a Bible in the other. <br>\n' +
        'Instead ... <b>articles</b> (“a”, “an”, or “the”) present in some of those passages in the <br>\n' +
        'Greek. ... events (e.g., flu outbreaks, <b>COVID</b>-19 <b>coronavirus</b>), corporate.',
      formattedUrl: 'www.cs.ubc.ca/~knorr/public/comparison_of_eschat_models.pdf',
      htmlFormattedUrl: 'www.cs.ubc.ca/~knorr/public/comparison_of_eschat_models.pdf',
      pagemap: [Object],
      mime: 'application/pdf',
      fileFormat: 'PDF/Adobe Acrobat'
    }
  ]
}

如何从上面的对象中提取所有的键和值?当我打印res.body['kind']时,它只打印对象中的第一个键。当我尝试打印res.body['items']时,它抛出了一个错误: items not defined。如何从对象中提取项目列表

编辑:我使用unirest节点模块从接口中获取内容:

代码语言:javascript
复制
  var req = unirest(
    "GET",
    "https://www.googleapis.com/customsearch/v1?key=<key here>&cx=<cx here>&q=covid+news+articles&g1=ca"
  );
  req.end(function (res) {
    if (res.error) throw new Error(res.error);

    console.log(res.body);
  });

  });

当我打印res.body时,我得到了上面的字典。我想使用res.body从上面的字典中提取所有键和它们各自的值

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-15 16:25:47

我找到解决方案了。我使用Object.keys(res.body)从对象中获取所有键。当我打印Object.keys(res.body)时,我得到了这个输出

代码语言:javascript
复制
[ 'kind', 'url', 'queries', 'context', 'searchInformation', 'items' ]

然后,我使用res.body.itemsres.body中打印items

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66204105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档