Home  >  Article  >  php教程  >  浅谈PHP中JSON数据操作

浅谈PHP中JSON数据操作

WBOY
WBOYOriginal
2016-06-06 20:02:41794browse

本文的知识点:1、JSON数据格式介绍,2、对数据编码成JSON格式,3、对JSON数据进行解码,并操作,有需要的小伙伴可以参考下。

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格式数据

得到的结果是[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;//使用成员访问的方式就可以得到结果

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

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

Statement:
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