PHP语言json_encode浮点数坑

in 前端 with 0 comment
<?php
$obj = ['a' => 10.00, 'b' => '你好'];
echo json_encode($obj); //{"a":10,"b":"\u4f60\u597d"}
echo json_encode($obj, JSON_UNESCAPED_UNICODE);//{"a":10,"b":"你好"}
echo json_encode($obj, JSON_PRESERVE_ZERO_FRACTION);//{"a":10.0,"b":"\u4f60\u597d"}
echo json_encode($obj, JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION);//{"a":10.0,"b":"你好"}

浮点数改变了。有些时候签名可能会报错,需要注意。

Comments are closed.