【问题标题】:Replace new JSON in javascript在 javascript 中替换新的 JSON
【发布时间】:2023-02-01 23:01:15
【问题描述】:

我想让 JSON 看起来像这样:

{"tokenId":1,"uri":"ipfs://bafy...","minPrice":{"type":"BigNumber","hex":"0x1a"},"signature":"0x51xxx"}

这是我的输出

 {
        "tokenId": "1",
        "uri": "ipfs://baf...",
        "minPrice": 0.03,
        "signature": "0x..."
    }

这是检索代码。

async function redeem(cid) {
  fetch(`http://localhost:4000/getDetails/${cid}`).then(response => {
    return response.json()
  }).then((async fef => {
    const obj = fef[0].minPrice.toString();
    const price = ethers.utils.parseUnits(obj,2);
    console.log(price)
  }))

 

我想让 minPrice 看起来和上面一样,所以我使用 ethers.utils.parseUnits。转换后,如何将现有的 minPrice 替换为 BigNumber minPrice?

【问题讨论】:

  • 0.03如何变成十六进制0x1a?该十六进制对应于十进制26

标签: javascript node.js json


【解决方案1】:

您可以通过指定基数将数字 minPrice 转换为十六进制字符串:

minPrice.toString(16)

所以你可能想要类似的东西

price.minPrice = {"type": "BigNumber","hex": price.minPrice.toString(16)}

【讨论】:

    猜你喜欢
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2018-07-20
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 2017-06-19
    相关资源
    最近更新 更多