PHP function json_decode decodes float value with zeros after point as int

匿名 (未验证) 提交于 2019-12-03 02:15:02

问题:

I have a JSON string that contains some key with the following value: 123.00. When I use json_decode function I get the decoded string where the previous key equal to 123, not to 123.00. Is there a way to correct decode such values without wrapping into quotes?

回答1:

This is currently being brought up as a PHP bug:

Bug Report: https://bugs.php.net/bug.php?id=50224

In the future, there may be functionality to pass a flag through the options parameter for stricter typing. For now, however, wrapping it in quotes will have to suffice.



回答2:

I do not think it is possible!



回答3:

//convert the json to a string before json_decode $res = preg_replace( '/next_cursor":(\d+)/', 'next_cursor":"\1"', $json ); 


回答4:

number_format($number, 2)  

output the number through that?



回答5:

You can use the JSON_BIGINT_AS_STRING option, for example:

$json = json_decode($input, true, 512, JSON_BIGINT_AS_STRING); 

Careful though, this only works with PHP 5.4+!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!