将带有斜线的JSON对象存储到Javascript对象中

时间:2018-06-22 18:55:33

标签: javascript json node.js superagent

我正在尝试从Node / Express应用程序的后端存储此api的'/ sources'端点的结果:https://newsapi.org/docs/endpoints/sources

结果像这样回来:

{
  "confirmation": "success",
  "data": "{\"status\":\"ok\",\"sources\":[{\"id\":\"abc- 
  news\",\"name\":\"ABC News\",\"description\":\"Your trusted source for 
  breaking news, analysis, exclusive interviews, headlines, and videos 
at ABCNews.com.\",\"url\":\"http://abcnews.go.com\",\"category\":\"general\",\"language\":\"en\",\"country\":\"us\"}
}

我不知道如何访问下面的内容,我认为这是由于转义斜线引起的,但是我找不到任何有关转换此内容的简单方法或为什么返回结果的信息。我知道我可以花一会儿时间来找出一种消除斜杠的方法,但是此API的其他端点将结果作为JSON对象返回,我使用时没有问题,并且我想确保自己没有遗漏一些我所做的事情有一个明显的大问题,并且在谷歌搜索时缺少哪些搜索词?

(在我的辩护中,我正在努力做到这一点,同时密切注意今天的3场世界杯小组赛比赛,所以....)

 superagent.get('https://newsapi.org/v2/sources')
    .set('X-Api-Key', '')
.then(results => {
  res.json({
    status: 'success',
    data: results.text
  })
})
.catch(err => {
      res.json({
       confirmation: 'fail',
       err: err
     })
   })

1 个答案:

答案 0 :(得分:0)

问题是results.text,它必须是results.body.sources