Php code for converting json format data _ PHP Tutorial

Source: Internet
Author: User
Tags php json
Php code for converting json format data. This article mainly introduces the json format data conversion program in php. We use the json_decode () and json_encode () functions to perform operations conveniently, this article describes how to operate the json format data conversion program in php. We use the json_decode () and json_encode () functions to perform operations conveniently, if you need to learn, refer to this example.

Step 1: Use the json_encode () function to convert data to json data.

The code is as follows:
// JSON format data is represented by arrays in php.
$ Arr = array (
'Firstname' => iconv ('gb2312', 'utf-8', 'sincerity '),
'Lastname' => iconv ('gb2312', 'utf-8', 'Do Not Disturb '),
'Contact '=> array (
'Email '=> 'fcwr @ bKjia. c0m ',
'Website' => 'http: // www. bKjia. c0m ',
)
);
// Encode the array into JSON data format
$ Json_string = json_encode ($ arr );
// JSON format data can be directly output
Echo $ json_string;
?>

This conversion function only supports UTF-8 format. if there is a Chinese character in the middle, you can use iconv or mb to convert it to the UTF-8 and then perform json_encode, so there will be no problem.

Step 2: parse the data. We also use a php json processing function json_decode (). The code is as follows:

The code is as follows:
// JSON format data is represented by arrays in php.
$ Arr = array (
'Firstname' => iconv ('gb2312', 'utf-8', 'sincerity '),
'Lastname' => iconv ('gb2312', 'utf-8', 'Do Not Disturb '),
'Contact '=> array (
'Email '=> 'fcwr @ bKjia. c0m ',
'Website' => 'http: // www. bKjia. c0m ',
)
);
// Encode the array into JSON data format
$ Json_string = json_encode ($ arr );
// Decodes JSON data. the decoded data is not in JSON format and cannot be output directly using echo.
$ Obj = json_decode ($ json_string );
// Forcibly convert to array format
$ Arr = (array) $ obj;
// Call the data in the array
Echo iconv ('utf-8', 'gb2312', $ arr ['firstname']);
Echo'
';
// Output array structure
Print_r ($ arr );
?>

Okay, the instance will talk about it here.
Json_decode () refer to http://www.bKjia. c0m/phper/18/32827 .htm
Json_encode () reference http://www.bKjia. c0m/phper/18/32827 .htm

The functions merge () and json_encode () are much easier to operate. you need to learn more...

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.