Json_decode data loss
I have a JSON string (external file) that has an element that can be FALSE or TRUE as its value. The file has true or false IS. However, after using json_decode on it, true or false is lost. Why?
JSON is valid, it is made of many blocks
{
"surroundedDebuff":true,
"citizenId":108981,
"citizenship":19,
"berserk":true,
"defenderSide":false,
"weapon":0,
"time":"25-03-2012 16:07:13:442",
"damage":65
}
(this is repeated many times) the check is a simple print_r.
+3
source to share
1 answer
print_r does not display types, so it displays 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)
}
+3
source to share