Convert string variables into JSON format in PHP

Source: Internet
Author: User

1  <? PHP 2  Header ("CONTENT-TYPE:TEXT/HTML;CHARSET=UTF-8;" ); 3  $arr Array (' Version_code ' =>2, ' version_name ' = ' updateversion ', ' versoin_desc ' + ' Update the map function ', ' versoin_path ' + ' http ://nnddkj.com/busiot/apk/busiot.apk '); 4  echo json_encode ($arr); 5  ? >

As the code above, the string variable is converted to JSON format output, but the output is as follows:

{"Version_code": 2, "Version_name": "Updateversion", "Versoin_desc": "\U66F4\U65B0\U4E86\U5730\U56FE\U529F\U80FD", " Versoin_path ":" http:\/\/nnddkj.com\/busiot\/apk\/busiot.apk "}

That is, all the Chinese in the array are missing after the Json_encode or appear \u2353.

The workaround is to use the UrlEncode () function to handle the following, before Json_encode, all the contents of the array are processed with UrlEncode (), and then converted to a JSON string with Json_encode (). Finally, I use UrlDecode () to turn the encoded Chinese back.

 1  <? php  2  header  ("content-type:text/html; Charset=utf-8; "  3   $arr  =  (' Version_code ' =>2, ' version_name ' = ' updateversion ', ' versoin_desc ' = urlencode  (' Updated map function '), ' Versoin_path ' =>urlencode  (' http://nnddkj.com/BusIot/APK/ busiot.apk '  4   UrlDecode  (Json_encode ( $arr   5 ? 

Output: {"Version_code": 2, "Version_name": "Updateversion", "Versoin_desc": "Updated map function", "Versoin_path": "/HTTP// nnddkj.com/busiot/apk/busiot.apk "}

Get..

Attached: Json_decode encodes a JSON-formatted string, while Json_encode JSON-encodes the variable

Json_decode - encode a string in JSON format

Description
Mixed Json_decode (String $json [, bool $assoc])
Accepts a JSON-formatted string and converts it to a PHP variable, $ASSOC, when this argument is TRUE, returns an array instead of an object

Json_encode: detailed asked Niang

Convert string variables into JSON format in PHP

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.