note 86513 added to function.json-encode
| From: | Garrett at osu1 dot php dot net | Date: | Wed, 22 Oct 2008 18:17:33 +0000 |
| Subject: | note 86513 added to function.json-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145605@lists.php.net to get a copy of this message | ||
A note about json_encode automatically quoting numbers:
It appears that the json_encode function pays attention to the data type of the value. Let me
explain what we came across:
We have found that when retrieving data from our database, there are occasions when numbers appear
as strings to json_encode which results in double quotes around the values.
This can lead to problems within javascript functions expecting the values to be numeric.
This was discovered when were were retrieving fields from the database which contained serialized
arrays. After unserializing them and sending them through the json_encode function the numeric
values in the original array were now being treated as strings and showing up with double quotes
around them.
The fix: Prior to encoding the array, send it to a function which checks for numeric types and casts
accordingly. Encoding from then on worked as expected.
----
Server IP: 208.69.120.35
Probable Submitter: 70.98.73.30
----
Manual Page -- http://www.php.net/manual/en/function.json-encode.php
Edit -- https://master.php.net/note/edit/86513
Del: integrated -- https://master.php.net/note/delete/86513/integrated
Del: useless -- https://master.php.net/note/delete/86513/useless
Del: bad code -- https://master.php.net/note/delete/86513/bad+code
Del: spam -- https://master.php.net/note/delete/86513/spam
Del: non-english -- https://master.php.net/note/delete/86513/non-english
Del: in docs -- https://master.php.net/note/delete/86513/in+docs
Del: other reasons-- https://master.php.net/note/delete/86513
Reject -- https://master.php.net/note/reject/86513
Search -- https://master.php.net/manage/user-notes.php