Home > Backend Development > PHP Tutorial > Use of JSON in php_PHP tutorial

Use of JSON in php_PHP tutorial

WBOY
Release: 2016-07-13 10:29:27
Original
881 people have browsed it

Starting from version 5.2, PHP natively provides json_encode() and json_decode() functions, the former is used for encoding, and the latter is used for decoding.

json_encode()                                                                                       This function is mainly used to convert arrays and objects into json format.

Output result:

<span>$arr</span> = <span>array</span> ('a'=>'a','b'=>'b','c'='c','d'=>'d','e'='e'<span>);
</span><span>echo</span> json_encode(<span>$arr</span>);
Copy after login

json only accepts utf-8 encoded characters, and the parameters of json_encode() must be utf-8 encoded.

Use of JSON in php_PHP tutorial Output result:

<span>class</span><span> person
{
    </span><span>public</span> <span>$name</span><span>;
    </span><span>public</span> <span>$age</span><span>;
    </span><span>public</span> <span>$height</span><span>;
    </span><span>function</span> __construct(<span>$name</span>,<span>$age</span>,<span>$height</span><span>)
    {
        </span><span>$this</span>->name = <span>$name</span><span>;
        </span><span>$this</span>->age = <span>$age</span><span>;
        </span><span>$this</span>->height = <span>$height</span><span>;    
    }   
}

</span><span>$obj</span> = <span>new</span> person("zhangsan",20,100<span>);
</span><span>$foo_json</span> = json_encode(<span>$obj</span><span>);
</span><span>echo</span> <span>$foo_json</span>;
Copy after login

When the attributes in the class are private variables, they will not be output.

Use of JSON in php_PHP tutorial

json_decode()                                                                                     

This function is used to convert json text into the corresponding PHP data structure. Output result:

Normally, json_decode() always returns a PHP object.

<span>$json</span> = '{"a":"hello","b":"world","c":"zhangsan","d":20,"e":170}'<span>;
</span><span>var_dump</span>(json_decode(<span>$json</span>));
Copy after login

converted into array:

Use of JSON in php_PHP tutorial

<span>$json</span> = '{"a":"hello","b":"world","c":"zhangsan","d":20,"e":170}'<span>;
</span><span>var_dump</span>(json_decode(<span>$json</span>,ture));
Copy after login

Use of JSON in php_PHP tutorial

Please indicate the source for reprinting: http://www.cnblogs.com/yydcdut/p/3751141.html

http://www.bkjia.com/PHPjc/776511.html

www.bkjia.com

true

TechArticleStarting from version 5.2, PHP natively provides json_encode() and json_decode() functions. The former is used for encoding, and the latter for decoding. json_encode() This function is mainly used to convert arrays and objects into...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template