更新时间:2023-02-23 11:30:12
我做了一个非常快速的测试:
$a = 数组('id' =>152,'另一个' =>'测试','另一个' =>456,);$json = json_encode($a);回声 $json;
如果我没记错的话,这似乎就像你描述的那样?
我得到的输出:
{"id":152,"another":"test","ananother":456}
因此,在这种情况下,整数尚未转换为字符串.
不过,这可能取决于我们使用的 PHP 版本:根据 PHP 的版本,已经纠正了几个与 json_encode 相关的错误...
本次测试是在 PHP 5.2.6 下进行的;我在 PHP 5.2.9 和 5.3.0 上得到了同样的东西;不过,我没有另一个 5.2.x 版本可以测试:-(
您使用的是哪个版本的 PHP?或者您的测试用例是否比您发布的示例更复杂?
也许一个关于 http://bugs.php.net/ 的错误报告可能与此相关?例如,Bug #40503:json_encode 整数转换与 PHP 不一致?>
也许 Bug #38680 你也感兴趣,顺便说一句?
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 :
$a = array(
'id' => 152,
'another' => 'test',
'ananother' => 456,
);
$json = json_encode($a);
echo $json;
This seems to be like what you describe, if I'm not mistaken ?
And I'm getting as output :
{"id":152,"another":"test","ananother":456}
So, in this case, the integers have not been converted to string.
Still, this might be dependant of the version of PHP we are using : there have been a couple of json_encode related bugs corrected, depending on the version of PHP...
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 ?
Maybe Bug #38680 could interest you too, btw ?