You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在fetch()中使用json-with-bigint解析Go返回的大int64整数

解决方案

首先安装json-bigint库(正确包名是这个,不是你提到的json-with-bigint):

npm install json-bigint

然后修改你的Vue方法,核心是放弃浏览器自带的response.json()解析,改用json-bigint处理响应文本,这样超大int64会被转为JS的BigInt类型,避免精度丢失:

import JSONBig from 'json-bigint'

// ...

fetchList() {
    console.log("fetchList called");
    const url = "/List"
    fetch(url, { 
        credentials: "same-origin",
    })
    .then(response => response.text()) // 先获取响应文本
    .then(text => {
        // 用json-bigint解析文本,大整数自动转为BigInt
        const data = JSONBig.parse(text);
        this.servers = data.servers;
    })
    .catch(err => console.log(err));
    console.log(this.list);
},

模板显示注意事项

因为Vue直接渲染BigInt可能会报错,所以在模板里需要把BigInt转为字符串:

<template>
  <div v-for="server in servers" :key="server.id.toString()">
    {{ server.id.toString() }}
  </div>
</template>

备选方案(如果后端可修改)

如果你的Golang后端允许调整,可以直接把大型int64字段转为字符串返回,前端就不需要额外引入库处理,直接用response.json()即可。

内容的提问来源于stack exchange,提问作者farhany

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 11:36:00