首页 > 解决方案 > 接收长整数值时出现json值错误

问题描述

我正在使用大整数数组向服务器发送数组 JSON 请求,当我收到它们并解码 JSON 时,它将整数值转换为科学记数法格式,我正在使用 php 函数。

{
    "friend_ids": [2719267598135496,2719267598135497]
}

(
    [friend_ids] => Array
        (
            [0] => 2.7192675981355E+15
            [1] => 2.7192675981355E+15
        )

)

这是我收到此值的 JSON 请求。我希望这与我发送到服务器的方式相同

标签: phpjson

解决方案


PHP 有时会遇到较大数字的问题。幸运的是, json_decode 有一个选项可以将大数转换为字符串:

json_decode($json, TRUE, 512, JSON_BIGINT_AS_STRING);

将返回这个:

array(1) {
  ["friend_ids"]=>
  array(2) {
    [0]=>
    string(16) "2719267598135496"
    [1]=>
    string(16) "2719267598135497"
  }
}

推荐阅读