JSON generation in PHP

Source: Internet
Author: User
How does PHP generate JSON without unioncode encoding conversion? For example, in json, $ a [& #039; name & #039; & amp; gt; & #039; Zhang San & #039 ;, & #039; age & #039; & amp; gt; 20]; after json_encode & #039; Zhang San & #039; will become & #039; u97asfddd & #039; how can we enable this encoding? How does PHP generate JSON without unioncode encoding conversion?

For example, in json, $ a = ['name' => 'zhang san', 'age' => 20]; after json_encode, 'zhang san' will become
'/U97asfddd/': how can we replace 'zhangsan' with '/u?

Reply content:

How does PHP generate JSON without unioncode encoding conversion?

For example, in json, $ a = ['name' => 'zhang san', 'age' => 20]; after json_encode, 'zhang san' will become
'/U97asfddd/': how can we replace 'zhangsan' with '/u?

JSON_UNESCAPED_UNICODE can be used only in php5.4 or later versions.

PHPfunction json_encode_wrapper ($result){    if(defined('JSON_UNESCAPED_UNICODE')){        return json_encode($result,JSON_UNESCAPED_UNICODE|JSON_NUMERIC_CHECK);    }else {        return preg_replace(            array("#\\\u([0-9a-f][0-9a-f][0-9a-f][0-9a-f])#ie", "/\"(\d+)\"/",),            array("iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", "\\1"),            json_encode($result)        );    }}

Http://php.net/json_encode

echo json_encode($arr, JSON_UNESCAPED_UNICODE);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.