如何绕过或使 PHP json_decode 不改变我的非常大的整数值?
所以我在 WAMP 环境中使用 php 5.2.6。
我正在尝试使用 json_decode 函数将 json 字符串放入数组中。 JSON 来自其他地方的 REST API,因此我无法控制 JSON 字符串的格式。下面是我尝试使用的 json 字符串之一的示例:
[{
"webinarKey":795855906,
"sessionKey":100000000041808257,
"startTime":"2011-12-16T13:56:15Z",
"endTime":"2011-12-16T14:48:37Z",
"registrantsAttended":2
}]
我特别在此处的 sessionKey 值之后。 PHP 将该值视为浮点数,我似乎无法执行任何操作来检索原始值。
我已经尝试过以下操作:
json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
# This produces the following error because my php version isn't up to snuff and I
# can't upgrade to the version required
# Warning: json_decode() expects at most 2 parameters, 4 given
我也尝试过这个:
$json_obj = json_decode($json, true);
number_format($json_obj[0]["sessionKey"], 0, '.', '');
# This results in precision issues where the value was 100000000041808257
# but is number_formated out as 100000000041808256
正如我所说,升级到 php 5.4(支持 4 参数 json_decode 调用)不是一个选项。请帮忙!
谢谢!
So I'm using php 5.2.6 in a WAMP environment.
I'm trying to use the json_decode function to make a json string into an array. The JSON is coming from a REST API elsewhere so I have no control over formatting of the JSON string. Here is an example of one of the json strings I'm trying to use:
[{
"webinarKey":795855906,
"sessionKey":100000000041808257,
"startTime":"2011-12-16T13:56:15Z",
"endTime":"2011-12-16T14:48:37Z",
"registrantsAttended":2
}]
I'm specifically after the sessionKey value here. PHP is treating the value as a float and I can't seem to do anything to retrieve the original value.
I've tried the following:
json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
# This produces the following error because my php version isn't up to snuff and I
# can't upgrade to the version required
# Warning: json_decode() expects at most 2 parameters, 4 given
I've also tried this:
$json_obj = json_decode($json, true);
number_format($json_obj[0]["sessionKey"], 0, '.', '');
# This results in precision issues where the value was 100000000041808257
# but is number_formated out as 100000000041808256
As I said, upgrading to php 5.4 (where the 4 parameter json_decode call is supported) isn't an option. Please help!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了保证 JSON 规范的质量,请使用:
To quality JSON spec use:
感谢@Scott Gottreu 和@pospi。
答案位于 此问题的已接受答案的最后评论中。
使用 preg_replace() 函数将所有整数值用引号引起来。
实际上,在测试上面的行之后,它用浮点数作为值来搞乱 JSON,为了解决这个问题,我使用以下命令将所有数字(整数或浮点数)括在引号中:
Thanks @Scott Gottreu and @pospi.
The answer was in the last comment for the accepted answer on this question.
Use the preg_replace() function to surround all integer values with quotes.
Actually after testing the above line it screws up JSON with floating point numbers in as values so to fix that issue I used the following to just enclose all numbers (integer or floating point numbers) in quotes:
与此同时,PHP 已经解决了这个问题……好吧,不知何故。从 PHP 5.4 左右开始,他们添加了一个选项,该选项的作用正是上面发布的正则表达式解决方案的作用:
512 指的是默认的最大嵌套深度。
Meanwhile, PHP has fixed this problem ... well, somehow. Starting sometime around PHP 5.4 they added an option which does the just what the Regex solutions posted above do:
The 512 refers to the default maximum nesting depth.
(我在这里重新发布另一个问题的答案(Handling big user IDs returned by FQL in PHP),因为这个更通用;与 Facebook / FQL 无关。)
PHP 方面的一个主要疏忽是浏览器不会因 BigInt(64 位)开始的整数而阻塞,但在此之前(53 位)位)。当 JSON 不支持 BigInt 时,将 BigInt 引入现代浏览器并没有多大帮助。所以我正在努力解决这个问题。
经常引用的使用
preg_replace()
的版本不考虑索引数组中的大整数。我的方法是处理解码后的数组(在可能将其重新编码为字符串之前):数字 9007199254740991 是 JavaScript 的 Number.MAX_SAFE_INTEGER 值。在这里阅读相关内容:https://developer .mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
(I'm re-posting my answer from another question (Handling big user IDs returned by FQL in PHP) here, because this is more generic; not related to Facebook / FQL.)
A major oversight on PHP's part is that browsers don't choke on integers starting with BigInt (64 bits), but before that (53 bits). The introduction of BigInt to modern browsers doesn't help much, when JSON does not support it. So I am trying to remedy that.
The often cited versions using
preg_replace()
do not account for large integers in indexed arrays. My approach is to handle the decoded array (before potentially re-encoding it to a string):The number 9007199254740991 is what JavaScript has as its Number.MAX_SAFE_INTEGER value. Read about that here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER