PHP scenario, you need to convert the array to a JSON string, you need to use PHP's own json_encode function;
But when the array contains Chinese strings, the result of the transfer is the following:
1<?PHP2 3 $TMPARR=Array(4' Name ' = ' trousers ',5' Color ' = ' blue ',6' Size ' = ' XL ',7' title ' and ' Aged mens trousers '8 );9 Ten $tmpJson= Json_encode ($TMPARR); One A Echo $tmpJson;
Output: {"name": "\u957f\u88e4", "Color": "Blue", "Size": "XL", "title": "\u4e2d\u5e74\u7537\u88c5 \u957f\u88e4"}
Need to Chinese not be converted, only need to give Json_encode function to pass in a parameter Json_unescaped_unicode can, as follows:
1<?PHP2 3 $TMPARR=Array(4' Name ' = ' trousers ',5' Color ' = ' blue ',6' Size ' = ' XL ',7' title ' and ' Aged mens trousers '8 );9 Ten $tmpJson= Json_encode ($TMPARR,json_unescaped_unicode); One A Echo $tmpJson;
Output: {"name": "Trousers", "color": "Blue", "Size": "XL", "title": "Middle-aged men's trousers"}
However, the above parameter json_unescaped_unicode is not supported in PHP version <5.4.0 and can be resolved in the following ways
1 $TMPARR=Array(2' Name ' = ' trousers ',3' Color ' = ' blue ',4' Size ' = ' XL ',5' title ' and ' Aged mens trousers '6 );7 8 $tmpJson= Json_encode ($TMPARR);9 $tmpJson=Preg_replace_callback("#\\\u ([0-9a-f]{4}) #i",function($matchs){Ten return Iconv(' Ucs-2be ', ' UTF-8 ',Pack(' H4 ',$matchs[1])); One},$tmpJson); A - Echo $tmpJson;
Output: {"name": "Trousers", "color": "Blue", "Size": "XL", "title": "Middle-aged men's trousers"}
Finally, you can encapsulate a function (mainly the test environment is different from the production environment, so encapsulate a function that can be used only in one way depending on your PHP environment):
1<?PHP2 $TMPARR=Array(3' Name ' = ' trousers ',4' Color ' = ' blue ',5' Size ' = ' XL ',6' title ' and ' Aged mens trousers '7 );8 9 functionJson_encode_array ($array){Ten if(Version_compare(php_version, ' 5.4.0 ', ' < ')){ One $str= Json_encode ($array); A $str=Preg_replace_callback("#\\\u ([0-9a-f]{4}) #i",function($matchs){ - return Iconv(' Ucs-2be ', ' UTF-8 ',Pack(' H4 ',$matchs[1])); -},$str); the return $str; -}Else{ - returnJson_encode ($array,json_unescaped_unicode); - } + } - + EchoJson_encode_array ($TMPARR);
Output: {"name": "Trousers", "color": "Blue", "Size": "XL", "title": "Middle-aged men's trousers"}
PHP self-contained function json_encode