json_decode data loss

こ雲淡風輕ζ 提交于 2019-12-23 16:41:56

问题


I have a JSON string (external file) which has an element which can either have FALSE or TRUE as a value. In the file, the true or false IS there. However, after I use json_decode on it, the true or false is lost. Why?

The JSON is valid, it is made from many blocks of

{
   "surroundedDebuff":true,
   "citizenId":108981,
   "citizenship":19,
   "berserk":true,
   "defenderSide":false,
   "weapon":0,
   "time":"25-03-2012 16:07:13:442",
   "damage":65
}

(this repeated many times), the checking is a simple print_r.


回答1:


print_r doesn't show types, so it will display 0 for false and 1 for true. var_dump will show that the values are actually booleans.

$decoded = json_decode('{"surroundedDebuff":true,"citizenId":108981,"citizenship":19,"berserk":true,"defenderSide":false,"weapon":0,"time":"25-03-2012 16:07:13:442","damage":65}');

print_r($decoded);
var_dump($decoded);

Outputs:

stdClass Object
(
    [surroundedDebuff] => 1
    [citizenId] => 108981
    [citizenship] => 19
    [berserk] => 1
    [defenderSide] => 
    [weapon] => 0
    [time] => 25-03-2012 16:07:13:442
    [damage] => 65
)
object(stdClass)#1 (8) {
  ["surroundedDebuff"]=>
  bool(true)
  ["citizenId"]=>
  int(108981)
  ["citizenship"]=>
  int(19)
  ["berserk"]=>
  bool(true)
  ["defenderSide"]=>
  bool(false)
  ["weapon"]=>
  int(0)
  ["time"]=>
  string(23) "25-03-2012 16:07:13:442"
  ["damage"]=>
  int(65)
}


来源:https://stackoverflow.com/questions/9873579/json-decode-data-loss

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!