问题
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