【问题标题】:Converting a JSON sting into an object as if it were received from an API将 JSON 字符串转换为对象,就好像它是从 API 接收的一样
【发布时间】:2022-08-19 15:00:46
【问题描述】:

我有一个发出此 API 请求的 VueJS 应用程序:

  this.$axios
    .get(\'/api/v1/something/endpoint\')
    .then(success => {
      self.holdings = success.data
    })
    .finally(() => {
      self.searched = true
    })

我已经从 PROD 环境中的浏览器网络选项卡捕获了 JSON 响应,我想在本地用这个 JSON 替换 success.data 值。

这不能通过简单地将 JSON 作为字符串放入,我假设是因为 axios 库正在执行 JSON 到对象的转换。

我怎样才能把这个响应当作实际的 API 响应一样删除,以便我可以在本地使用生产数据进行测试?

NB。将我的本地机器指向生产 API 不是一种选择。

    标签: json api rest axios


    【解决方案1】:

    最后,我只是从本地文件中加载了 JSON blob:

    import _json from '@/assets/holdings.json'
    

    并用加载的对象替换传入的数据:

    self.holdings = _json
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 2019-08-27
      • 2020-05-27
      • 1970-01-01
      相关资源
      最近更新 更多