PHP 中使用 json_decode() 处理无符号整数
我意识到 PHP 不支持无符号整数,但我想知道如何解决 json_decode() 遇到的这个问题。当我解码包含大于 2^31 的无符号整数的 JSON 时,它们的结果为负,因为它们被视为有符号整数。我可以通过配置 php.ini 文件来纠正这个问题,还是只是有一些关于 json_decode 的问题我没有得到?
I realize that PHP does not support unsigned integers, but I'm wondering how to solve this issue I'm having with json_decode(). When I decode JSON that contains unsigned ints above 2^31, they come out negative, since they are being treated as signed ints. Is this something I can rectify by configuring my php.ini file or is there just something about json_decode that I'm not getting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 PHP BC 数学扩展!
You can use the PHP BC Math extension!
PHP 的开发版本似乎有一个计划选项
json_decode()
正是针对这个问题:JSON_BIGINT_AS_STRING
(请参阅手册中的示例#5),但目前似乎没有针对此问题的生产修复。另请参阅用户贡献的注释中的此评论。
有可能在编码端以某种方式输出字符串吗?
The development version of PHP seems to have a planned option to
json_decode()
exactly for this problem:JSON_BIGINT_AS_STRING
(see Example #5 in the manual) but there seems to be no production fix for this right now.See also this comment in the user contributed notes.
Any chance of outputting strings on the encoding end somehow?