How to use JSON in PHP, Phpjson How to use _php tutorial

Source: Internet
Author: User

How to use JSON in PHP, how to use Phpjson


Starting with version 5.2, PHP native provides the Json_encode () and Json_decode () functions, which are used for encoding, which is used for decoding.
Json_encode ()
This function is primarily used to convert arrays and objects into JSON format.

Copy the Code code as follows:
$arr = Array (' a ' = = ' A ', ' B ' = ' B ', ' c ' = ' C ', ' d ' = ' d ', ' e ' = ' e ');
echo Json_encode ($arr);

Output Result:
JSON accepts only utf-8 encoded characters, and Json_encode () parameters must be utf-8 encoded.

Class Person {public   $name;   public $age;   public $height;   function __construct ($name, $age, $height)   {     $this->name = $name;     $this->age = $age;     $this->height = $height;     }  

Output Result:
When a property in a class is a private variable, it is not output.
Json_decode ()
This function is used to convert the JSON text to the appropriate PHP data structure.

Copy the Code code as follows:
$json = ' {' A ': ' Hello ', ' B ': ' World ', ' C ': ' Zhangsan ', ' d ': ', ' E ': 170} ';
Var_dump (Json_decode ($json));

Output Result:
Typically, Json_decode () always returns a PHP object.
To an array:

Copy the Code code as follows:
$json = ' {' A ': ' Hello ', ' B ': ' World ', ' C ': ' Zhangsan ', ' d ': ', ' E ': 170} ';
Var_dump (Json_decode ($json, ture));

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/992544.html www.bkjia.com true http://www.bkjia.com/PHPjc/992544.html techarticle The use of JSON in PHP, Phpjson use method starting from the 5.2 version, PHP native provides Json_encode () and Json_decode () functions, the former for encoding, the latter for decoding. Json_encode () the ...

  • 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.