【问题标题】:json_decode in php returns in unexpected form [duplicate]php中的json_decode以意外的形式返回[重复]
【发布时间】:2014-11-06 04:51:05
【问题描述】:

使用 json_decode 时,我得到的字段包含意外字符串。 我的 json 文件是:

{
buyer_accepts_marketing: false,
cart_token: "eeafa272cebfd4b22385bc4b645e762c",
id: 327474488104976400,
}

而我的相关php代码是

$info = json_decode($file,true);
print $info["id"];
echo "<br>";
print $info["cart_token"];

我得到的输出是

3.2747448810498E+17

eeafa272cebfd4b22385bc4b645e762c

为什么我没有得到正确的 id 值?

【问题讨论】:

  • 这个值是否来自数据库,我的意思是一个服务带来了这些数据。?
  • 准确来说,json文件是由shopify webhook生成的。
  • id:“327474488104976400”。只需更改文件即可。
  • id类型为整数,当前值超出整数范围。
  • 对字符串进行强制转换,因为该值是一个浮点数,虽然看起来像一个整数

标签: php json


【解决方案1】:

发生这种情况是因为id 的值超出了范围。尝试将其创建为字符串。如果改成字符串就可以正常工作了。

Integer overflow

If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.

【讨论】:

  • Tkz.. preg_replace 是最好的解决方案。
猜你喜欢
  • 2012-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-06
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 2017-05-02
相关资源
最近更新 更多