There is no problem not worth solving, and no technology not worth learning!

In PHP, insert a JSON string into MySQL, Chinese has been encoded

在 PHP 中,插入 json 字符串(其中的中文已经被编码)至 MySQL 中,中文已经被编码。

1. In PHP, insert the JSON string (the Chinese has been encoded) into MySQL, and the Chinese has been encoded. as shown in Figure 1

在 PHP 中,插入 json 字符串(其中的中文已经被编码)至 MySQL 中,中文已经被编码。
Figure 1

insert into
  `table` (
    `column`,
  )
values
  (
    '{\"custom\":\"\",\"result\":{\"ack\":\"false\",\"attr1\":\"\",\"attr2\":\"\",\"channel_code\":\"\",\"childList\":[],\"delay_type\":\"\",\"is_changenumbers\":\"\",\"is_delay\":\"\",\"is_remote\":\"\",\"is_residential\":\"\",\"label_url\":\"\",\"message\":\"%E7%94%B3%E6%8A%A5%E4%BB%B7%E5%80%BC%E5%BF%85%E9%A1%BB%E5%A4%A7%E4%BA%8E0\",\"order_id\":\"\",\"order_privatecode\":\"\",\"order_transfercode\":\"\",\"orderpricetrial_amount\":\"\",\"orderpricetrial_currency\":\"\",\"post_customername\":\"\",\"product_tracknoapitype\":\"\",\"reference_number\":\"\",\"return_address\":\"\",\"tracking_number\":\"\"}}'
  )


2. Try to url decode the entire JSON string directly. Instead of converting JSON to an array, then traversing the array, url decoding each value, and finally encode the array into a JSON string. Use the online tool to try url decoding, and finally format it again, and the format is successful, and it is found that this scheme is feasible. as shown in Figure 2

尝试直接将整个 json 字符串进行 URL解码。而不是将 json 转换为数组,然后再遍历数组,将每一个值进行 URL解码,最后再将数组编码为 json 字符串。使用在线工具尝试 URL 解码,最后再格式化,格式化成功,发现此方案可行。
Figure 2

3. The last implementation in the code is as follows


isset($result['transport_back_result']) ? urldecode($result['transport_back_result']) : ''


4. Check the json string in mysql to confirm that the Chinese has been decoded by the url, which is in line with expectations. as shown in Figure 3

查看 MySQL 中的 json 字符串,确认中文已经被 URL解码,符合预期
Figure 3

5. However, there are still cases where Chinese is not displayed normally in the request parameter. The reason is that the request parameters are implemented with json_encode($requestParsData) and then return. In the end, it was decided to decode json first, then json encoding, and add the parameter json_unescaped_unicode when encoding. JSON_UNESCAPED_UNICODE means multibyte unicode characters are literally encoded (default is encoded to \UXXXX).

 


{"trade_type":"ZYXT","order_returnsign":"N","duty_type":"DDU","cargo_type":"B","consignee_name":"\u5ba2\u6237\u59d3\u540d"}




{
  "trade_type": "ZYXT",
  "order_returnsign": "N",
  "duty_type": "DDU",
  "cargo_type": "B",
  "consignee_name": "客户姓名"
}



json_encode(json_decode($str, true), JSON_UNESCAPED_UNICODE);



{"trade_type":"ZYXT","order_returnsign":"N","duty_type":"DDU","cargo_type":"B","consignee_name":"客户姓名"}


Need long-term technical maintenance or remote troubleshooting?

I am a PHP / Go backend engineer with 15+ years of experience, focused on existing system maintenance, bug fixing, performance optimization, server troubleshooting, WordPress maintenance, and small feature iterations.

If your project is facing any of the following issues, we can start with a small troubleshooting task first:

  • ✅ PHP / Laravel / Yii2 legacy systems without active maintenance
  • ✅ Go / Gin backend APIs that need troubleshooting or optimization
  • ✅ Slow, broken, or unstable WordPress websites
  • ✅ Nginx / MySQL / Redis / Linux server issues
  • ✅ CDN / Cloudflare / DNS / HTTPS configuration problems
  • ✅ Long-term remote technical support or part-time maintenance

More details: About Me & Collaboration

WeChat: 13980074657
Email: shuijingwanwq@gmail.com
Telegram: @shuijingwan
GitHub: https://github.com/shuijingwan

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.