JSON in PHP using _php tutorial

Source: Internet
Author: User
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.

$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 ;         $this $age ;         $this $height ;        }   } $obj New person ("Zhangsan", 20,100); $foo _json = Json_encode ($obj); Echo $foo _json;

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.

$json = ' {' A ': ' Hello ', ' B ': ' World ', ' C ': ' Zhangsan ', ' d ': ', ' e ': "; Var_dump (Json_decode ($json));

Output Result:

Typically, Json_decode () always returns a PHP object.

To an array:

$json = ' {' A ': ' Hello ', ' B ': ' World ', ' C ': ' Zhangsan ', ' d ': ', ' e ': "; Var_dump (Json_decode ($json, ture));

Reprint Please specify source: http://www.cnblogs.com/yydcdut/p/3751141.html

http://www.bkjia.com/PHPjc/776511.html www.bkjia.com true http://www.bkjia.com/PHPjc/776511.html techarticle 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 to ...

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