$arr = array(
'status' => true,
'errMsg' => '',
'member' =>array(
array(
'name' => '李逍遥',
'gender' => '男'
),
array(
'name' => '赵灵儿',
'gender' => '女'
)
)
);
echo json_encode($arr);
上面打印结果是:
{"status":true,"errMsg":"","member":[{"name":"\u674e\u900d\u9065","gender":"\u7537"},{"name":"\u8d75\u7075\u513f","gender":"\u5973"}]}
上面结果如果是中文就显示unicode编码,例如\u975a\u4ed4,看起了不舒服
php5.4 以后,json_encode增加了JSON_UNESCAPED_UNICODE , JSON_PRETTY_PRINT 等几个常量参数。使显示中文与格式化更方便
echo json_encode($arr,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);使用这个打印结果是:
{ "status": true, "errMsg": "", "member": [ { "name": "李逍遥", "gender": "男" }, { "name": "赵灵儿", "gender": "女" } ] }
中文就是中文看起了就舒服多了
如果是echo json_encode($arr,JSON_UNESCAPED_UNICODE);结果是:
{"status":true,"errMsg":"","member":[{"name":"李逍遥","gender":"男"},{"name":"赵灵儿","gender":"女"}]}
结果和echo json_encode($arr,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);是一样的的,区别就是没有json格式化
JSON_UNESCAPED_UNICODE是中文不转码,JSON_PRETTY_PRINT是json串格式化