Code for json format data conversion in php

Source: Internet
Author: User
Tags php json
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 mainly introduces the json format data conversion program in php. We use the json_decode () and json_encode () functions to perform operations conveniently.

The first step is to use the json_encode () function to convert the data into json data. the code is as follows:

  1. // JSON format data is represented by arrays in php.
  2. $ Arr = array (
  3. 'Firstname' => iconv ('gb2312', 'utf-8', 'sincerity '),
  4. 'Lastname' => iconv ('gb2312', 'utf-8', 'Do Not Disturb '),
  5. 'Contact '=> array (
  6. 'Email '=> 'fcwr @ phpfensi.com ',
  7. 'Website' => 'http: // www.phpfensi.com ',
  8. )
  9. );
  10. // Encode the array into JSON data format
  11. $ Json_string = json_encode ($ arr );
  12. // JSON format data can be directly output
  13. Echo $ json_string;
  14. ?>

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 and use a php json processing function json_decode (). The code is as follows:

  1. // JSON format data is represented by arrays in php.
  2. $ Arr = array (
  3. 'Firstname' => iconv ('gb2312', 'utf-8', 'sincerity '),
  4. 'Lastname' => iconv ('gb2312', 'utf-8', 'Do Not Disturb '),
  5. 'Contact '=> array (
  6. 'Email '=> 'fcwr @ phpfensi.com ',
  7. 'Website' => 'http: // www.phpfensi.com ',
  8. )
  9. );
  10. // Encode the array into JSON data format
  11. $ Json_string = json_encode ($ arr );
  12. // Decodes JSON data. the decoded data is not in JSON format and cannot be output directly using echo.
  13. $ Obj = json_decode ($ json_string );
  14. // Forcibly convert to array format
  15. $ Arr = (array) $ obj;
  16. // Call the data in the array
  17. Echo iconv ('utf-8', 'gb2312', $ arr ['firstname']);
  18. Echo'
    ';
  19. // Output array structure
  20. Print_r ($ arr );
  21. ?>

Okay, the instance is here.

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.