duanmei1694 2017-11-06 15:22
浏览 65
已采纳

如何为json编码选择正确的字符集,因此不需要JSON_UNESCAPED_UNICODE

i'm really new to work with JSON format. I learnd a lot of things - and atm i stuck with the UNICODE-thing. I have a string $str='Läuse';

if I look what charset this string is I use echo mb_detect_encoding($str); result is UTF-8. Now i create a array and detect again the charset is UTF-8

 $json_array = [
        'test' => $str
    ];
    echo mb_detect_encoding($json_array['test']);

When I echo the array with json_encode it comes to {"test":"L\u00e4use"} instead of {"test":"Läuse"}

After searching and reading a lot about php and unicode i'm more and more confused. I learnd that JSON_UNESCAPED_UNICODE will output my json_encode as I want.

My question is, am I doing something wrong that I have to use JSON_UNESCAPED_UNICODE? Do I use the wrong charset ?

Hope you can help me out with that

  • 写回答

1条回答 默认 最新

  • doufei2662 2017-11-06 15:24
    关注

    PHP just defaults to outputting ASCII compatible JSON which is unlikely to be mangled by encoding errors (although it comes at the cost of readability and some bytes).

    You aren't doing anything wrong. JSON_UNESCAPED_UNICODE just is off by default.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改