note 105892 added to function.json-decode
| From: | alexvonweiss at googlemail dot com | Date: | Fri, 23 Sep 2011 15:49:11 +0000 |
| Subject: | note 105892 added to function.json-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183368@lists.php.net to get a copy of this message | ||
Consider that JSON can differ between int and string. So
<?php
var_dump(json_decode('{"foo": 12}'));
// array(1) { ["foo"]=> int(12) }
var_dump(json_decode('{"foo": "12"}'));
// array(1) { ["foo"]=> string(12) }
?>
Numbers that cannot be handled by integer seems to become float casted. This can be a problem if you
transfer big numbers like facebook ids over JSON. Either you avoid numbers by cast everything to
string before JSON.stringify or you have to use number_format if the value bacome a float value.
<?php
// test
$x = json_decode('{"foo": 123456789012345}');
echo sprintf('%1$f', $x->foo).PHP_EOL;
echo sprintf('%1$u', $x->foo).PHP_EOL;
echo sprintf('%1$s', $x->foo).PHP_EOL;
echo strval($x->foo).PHP_EOL;
echo (string) $x->foo.PHP_EOL;
echo number_format($x->foo, 0, '', '').PHP_EOL;
// output
123456789012345.000000 // printf %f
2249056121 // printf %u
1.2345678901234E+14 // printf %s
1.2345678901234E+14 // strval()
1.2345678901234E+14 // cast (string)
2249056121 // cast (int)
123456789012345 // number_format()
?>
----
Server IP: 69.147.83.197
Probable Submitter: 217.9.63.50
----
Manual Page -- http://www.php.net/manual/en/function.json-decode.php
Edit -- https://master.php.net/note/edit/105892
Del: integrated -- https://master.php.net/note/delete/105892/integrated
Del: useless -- https://master.php.net/note/delete/105892/useless
Del: bad code -- https://master.php.net/note/delete/105892/bad+code
Del: spam -- https://master.php.net/note/delete/105892/spam
Del: non-english -- https://master.php.net/note/delete/105892/non-english
Del: in docs -- https://master.php.net/note/delete/105892/in+docs
Del: other reasons-- https://master.php.net/note/delete/105892
Reject -- https://master.php.net/note/reject/105892
Search -- https://master.php.net/manage/user-notes.php