In PHP, insert a JSON string into MySQL, Chinese has been encoded
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
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
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
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":"客户姓名"}


