I am having one problem with the PHP json_encode function. It encodes numbers as strings (e.g. array('id' => 3 becomes "{ ["id": "3", ...) When js encounters these values, it interprets them as strings and numeric operations fail on them. Does anyone know some way to prevent json_encode from encoding numbers as strings? Thank you!
I've done a very quick test :
This seems to be like what you describe, if I'm not mistaken ? And I'm getting as output :
So, in this case, the integers have not been converted to string.
This test has been made with PHP 5.2.6 ; I'm getting the same thing with PHP 5.2.9 and 5.3.0 ; I don't have another 5.2.x version to test with, though :-( Which version of PHP are you using ? Or is your test-case more complex than the example you posted ? Maybe one bug report on http://bugs.php.net/ could be related ? For instance, Bug #40503 : json_encode integer conversion is inconsistent with PHP ?
|
|||||||||||||||
|
json_encode serializes some data structure in JSON format to be send across the network. Therefore all content will be of the type string. Just like when you receive some parameter from $_POST or $_GET. If you have to make numeric operations on the values sent, just convert them to int first (with the intval() function in PHP or parseInt() in Javascript) and then execute the operations. |
|||||||
|
Well, PHP json_encode() returns a string. You can use parseFloat() or parseInt() in the js code though:
|
|||||||||
|
I'm encountering the same problem (PHP-5.2.11/Windows). I'm using this workaround
which replaces all (non-negative, integer) numbers enclosed in quotes with the number itself ('"42"' becomes '42'). See also this comment in PHP manual. |
|||
|
The following test confirms that changing the type to string causes json_encode() to return a numeric as a JSON string (i.e., surrounded by double quotes). Use settype(arr["var"], "integer") or settype($arr["var"], "float") to fix it.
|
|||
|
I also had the same problem processing data from the database. Basically the problem is that the type in the array to convert in json, is recognized by PHP as a string and not as integer. In my case I made a query that returns data from a DB column counting row. The PDO driver does not recognize the column as int, but as strings. I solved by performing a cast as int in the affected column. |
|||
|
Note that from PHP 5.3.3 on, there's a flag for auto-converting numbers, while options parameter was added since PHP 5.3.0:
|
|||||||||||||||
|
For sake of completeness (as I can't add comments yet), let me also add this detail as another answer: (Edit: To be read after realizing that the source data (i.e. in the OP's case, database result set) could be the problem (by returning numeric columns as strings), and json_encode() in fact was not the source of the problem) Manual pages of both "mysql_fetch_array":
... and "mysql_ fetch_ row":
clearly states that; the entries in the returned array will be strings. (I was using the DB class in phpBB2 (yes I know, it's obsolete!), and "sql_fetchrow()" method of that class uses "mysql_fetch_array()") Not realizing that, I also ended up finding this question, and understanding the problem! :) As Pascal Martin stated above in his follow-up comments, I believe a solution that takes care of the "incorrect type" problem at the source (i.e. by using the "mysql_field_type()" function and doing the casting right after fetch, (or other fetch methods like "object"?)) would be the better in general. |
||||
|
try
But it just work on PHP 5.3.3. Look at this PHP json_encode change log http://php.net/manual/en/function.json-encode.php#refsect1-function.json-encode-changelog |
|||
|
I, likewise was reading from a DB (PostgreSQL) and everything was a string. We loop over each row and do things with it to build up our final results array, so I used
within the loop to force it to be an integer value. When I do |
|||
|
It turns out that this is a version-specific problem. Sometimes a pull from a MySql database will maintain the correct types. In older versions, it may return everything as a string. I wrote about it this morning. http://shakyshane.com/blog/output-json-from-php.html |
|||
|
If you are using Have a look at this question: fetching an Integer from DB using Zend Framework returns the value as a string |
|||
|