A brief discussion on JSON data operations in PHP_PHP tutorial

WBOY
Release: 2016-07-13 09:48:10
Original
965 people have browsed it

A brief discussion on JSON data operations in PHP

Knowledge points of this article: 1. Introduction to JSON data format, 2. Encoding data into JSON format, 3. Decoding JSON data , and operate it, friends in need can refer to it.

JSON, the full name is JavaScript Object Notation. It is a lightweight data exchange format based on the JavaScript programming language ECMA-262 3rd Edition-December 1999 standard, which is mainly used to exchange data with the server. Similar to XML, it is language independent and has great advantages in cross-platform data transmission

Create a new file json.php and perform the encode operation first:

 ?

1

2

3

4

//encode

//生成JSON格式数据

$arr = array(1,2,3,4,5,6,7,8,9,'Hello','PHP');

echo json_encode($arr);//json_encode:把一个对象转换成json格式数据

1

2

3

4

1

2

3

4

//decode 解码

$jsonStr = '{"h":"Hello","w":"World","0":[3,2,1]}';

$obj = json_decode($jsonStr);

echo $obj->h;//使用成员访问的方式就可以得到结果

//encode

//Generate JSON format data $arr = array(1,2,3,4,5,6,7,8,9,'Hello','PHP');

echo json_encode($arr);//json_encode: Convert an object into json format data
The result is [1,2,3,4,5,6,7,8,9,"Hello","PHP"] Do the decoding operation again:  ?
1 2 3 4
//decode decoding $jsonStr = '{"h":"Hello","w":"World","0":[3,2,1]}'; $obj = json_decode($jsonStr); echo $obj->h;//You can get the result using member access
After you know the simple usage method, you can try to capture API data, such as weather... The above is the entire content of this article, I hope you all like it. http://www.bkjia.com/PHPjc/1025315.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1025315.htmlTechArticleA brief discussion on JSON data operations in PHP Knowledge points of this article: 1. Introduction to JSON data format, 2. Encoding data into JSON format, 3. Decode and operate the JSON data, friends in need can...
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