简体   繁体   English

将循环结构转换为JSON-第三方API

[英]Converting circular structure to JSON - 3rd party API

I'm building out a project that is making a call to a blockchain API. 我正在构建一个正在调用区块链API的项目。 Unfortunately, the data I'm getting back is circular so while it works in Postman my server errors when trying to convert it to JSON. 不幸的是,我返回的数据是循环的,因此在Postman中工作时,尝试将其转换为JSON时服务器出错。 I tried using JSON.stringify but nothing changed. 我尝试使用JSON.stringify但没有任何变化。

Here's the controller function: 这是控制器功能:

blockchainController.search = (req, res) => {
  axios({
    method: 'GET',
    url: `https://chain.api.btc.com/v3/address/${req.body.address}/tx`
  })
  .then(data => {
    res.json({
      message: 'Transactions loaded',
      data: data
    })
  })
  .catch(err => {
    console.log(err);
    res.send(err);
  })
};

Any ideas for a workaround or a fix? 有任何解决方法或解决方案吗? I'd like to be able to send this data to my front-end but it ain't happening. 我希望能够将这些数据发送到我的前端,但是没有发生。

A solution could be to use a library designed to prune circular references. 一个解决方案可能是使用旨在修剪循环引用的库。

I happen to have built such library: https://github.com/Canop/JSON.prune 我恰好建立了这样的库: https : //github.com/Canop/JSON.prune

You can simply call it with 您可以简单地用

let json = JSON.prune(yourCircularObject);

This adds some "-pruned-" marks whenever a reference is ignored. 每当忽略引用时,都会添加一些"-pruned-"标记。

If you prefer a "silent" removal, you can do 如果您希望“无声”删除,可以执行

let json = JSON.prune(yourCircularObject, {prunedString: undefined });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM