Home > php教程 > php手册 > 浅谈PHP中JSON数据操作,浅谈phpjson数据

浅谈PHP中JSON数据操作,浅谈phpjson数据

WBOY
Release: 2016-06-13 08:59:17
Original
813 people have browsed it

浅谈PHP中JSON数据操作,浅谈phpjson数据

JSON,全称是JavaScript Object Notation。它是基于JavaScript编程语言ECMA-262 3rd Edition-December 1999标准的一种轻量级的数据交换格式,主要用于跟服务器进行交换数据。跟XML相类似,它独立语言,在跨平台数据传输上有很大的优势

新建文件json.php,先做encode操作:

 //encode
 //生成JSON格式数据
 $arr = array(1,2,3,4,5,6,7,8,9,'Hello','PHP');
 echo json_encode($arr);//json_encode:把一个对象转换成json格式数据

Copy after login

得到的结果是[1,2,3,4,5,6,7,8,9,"Hello","PHP"]

再来做decode操作:

 //decode 解码
 $jsonStr = '{"h":"Hello","w":"World","0":[3,2,1]}';
 $obj = json_decode($jsonStr);
 echo $obj->h;//使用成员访问的方式就可以得到结果

Copy after login

知道简单的使用方法后,可以尝试着抓API数据,比如天气...

以上所述就是本文的全部内容了,希望大家能够喜欢。

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template