【问题标题】:Storing JSON object with slashes into Javascript object将带有斜杠的 JSON 对象存储到 Javascript 对象中
【发布时间】:2018-12-02 07:06:14
【问题描述】:

我正在尝试从 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
     })
   })

【问题讨论】:

  • 我怀疑 res 已经是 json 了.. 所以不要再重新制作它 json 否则它会逃逸.. 或者stringify result.text ?
  • 嘿,您应该在发布此问题后重置您的 API 密钥,因为您的安全性现在受到威胁。 News-API 在帐户设置中具有生成新帐户的功能。
  • 预期输出是什么?
  • 不要对结果进行双重编码。 data: JSON.parse(results.text)

标签: javascript json node.js superagent


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    相关资源
    最近更新 更多