从Node.js中阅读JSON的Bigint字段
我正在尝试从 json
文件中读取 bigint
字段:
const fs = require("fs");
function reviver(key, value) {
return BigInt(value);
}
let rawdata = fs.readFileSync("input.json");
let numbers = JSON.parse(rawdata, reviver);
console.log(numbers);
我的输入json文件看起来像这样:
[
{"number": 19819, "divisor": 34},
{"number": 888, "divisor": 19},
{"number": 55555, "divisor": 126}
]
这是我遇到的错误:
$ node div.js
/home/oren/div.js:4
return BigInt(value);
^
SyntaxError: Cannot convert [object Object] to a BigInt
at BigInt (<anonymous>)
at Array.reviver (/home/oren/div.js:4:10)
at JSON.parse (<anonymous>)
at Object.<anonymous> (/home/oren/div.js:8:20)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
我在做什么错?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的值是对象
your value is object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#using_the_reviver_parameter