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, 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: |
Copy code |
<? Php // 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: |
Copy code |
<? Php // 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 '</br> '; // 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