未处理
php 项目开发经常遇到用 json_encode 处理参数然后加密去请求别的项目接口,如果其他项目是非 php 语言,容易在 json_encode 的时候出现中文转义和反斜杠转义的问题,导致加密参数出错。
$goods['name'] = '商品名称';
$goods['picture'] = 'http://localhost/mytest/images/test.jpg';
$goods['price'] = 19.9;
$goods['status'] = 1;
$goods['stock'] = 200;
echo json_encode($goods);
结果:
{"name":"\u5546\u54c1\u540d\u79f0","picture":"http:\/\/localhost\/mytest\/images\/test.jpg","price":19.9,"status":1,"stock":200}
处理中文转义
echo json_encode($goods);
结果:
{"name":"商品名称","picture":"http:\/\/localhost\/mytest\/images\/test.jpg","price":19.9,"status":1,"stock":200}
处理中文转义和反斜杠
echo json_encode($goods,320);
结果:
{"name":"商品名称","picture":"http://localhost/mytest/images/test.jpg","price":19.9,"status":1,"stock":200}
|