Some of my data are 64-bit integers. I would like to send these to a JavaScript program running on a page.

However, as far as I can tell, integers in most JavaScript implementations are 32-bit signed quantities.

我的两个 Select 似乎是:

  1. Send the values as strings
  2. Send the values as 64-bit floating point numbers

Option (1) isn't perfect, but option (2) seems far less perfect (loss of data).

How have you handled this situation?

推荐答案

This seems to be less a problem with JSON and more a problem with Javascript itself. What are you planning to do with these numbers? If it's just a magic token that you need to pass back to the website later on, by all means simply use a string containing the value. If you actually have to do arithmetic on the value, you could possibly write your own Javascript routines for 64-bit arithmetic.

用Javascript(以及JSON)表示值的一种方法是将数字拆分为两个32位的值,例如.

  [ 12345678, 12345678 ]

To split a 64-bit value into two 32-bit values, do something like this:

  output_values[0] = (input_value >> 32) & 0xffffffff;
  output_values[1] = input_value & 0xffffffff;

Then to recombine two 32-bit values to a 64-bit value:

  input_value = ((int64_t) output_values[0]) << 32) | output_values[1];

Json相关问答推荐

筛选JSON数组以使用Jolt仅保留具有最新日期/时间的条目

使用WSO2 JsonTransform Mediator对空值执行JsonExceptionUndeletedOperationException

如何使用JQ打印每个根级对象键一行?

当console.log返回TRUE值时,解析的JSON中的字段未定义

Allof Indide的JSON模式之一

当由.sh脚本执行时,AWS查询字符串不会提取任何数据

使用 jolt 变换压平具有公共列 JSON 的复杂嵌套

从包含 JSON 对象序列的文件中获取第一个 JSON 对象

使用 jq 获取所有嵌套键和值

将 json 转换为 jsonb 安全吗?

未知的META规范,无法验证.[规范v1.0.1]

Spark-SQL中的from_unixtime函数未能给出正确的输出

jq: Select 何时来自另一个数组的值与此 json 中的值匹配

如何使用 React 从 NASA IMAGES API 中解构所需信息

Go - JSON 验证抛出错误,除非我在 struct 中使用指针.为什么?

如何比较 JSON 文档并返回与 Jackson 或 Gson 的差异?

Swift - 将图像从 URL 写入本地文件

如何从Typescript 中的json响应中获取日期对象

是否可以将数据写入本地 json 文件,除了Angular 之外什么都没有?

在android中读取Json数组