Rumah > Artikel > pembangunan bahagian belakang > PHP学习笔记7-JSON数据操作,php学习笔记7-json_PHP教程
JSON,全称是JavaScript Object Notation。它是基于JavaScript编程语言ECMA-262 3rd Edition-December 1999标准的一种轻量级的数据交换格式,主要用于跟服务器进行交换数据。跟XML相类似,它独立语言,在跨平台数据传输上有很大的优势
新建文件json.php,先做encode操作:
<span>1</span> <span>//</span><span>encode </span><span>2</span> <span>//生成JSON格式数据</span> <span>3</span> <span>$arr</span> = <span>array</span>(1,2,3,4,5,6,7,8,9,'Hello','PHP'<span>); </span><span>4</span> <span>echo</span> json_encode(<span>$arr</span>);<span>//</span><span>json_encode:把一个对象转换成json格式数据</span>
得到的结果是[1,2,3,4,5,6,7,8,9,"Hello","PHP"]
再来做decode操作:
<span>1</span> <span>//</span><span>decode 解码</span> <span>2</span> <span>$jsonStr</span> = '{"h":"Hello","w":"World","0":[3,2,1]}'<span>; </span><span>3</span> <span>$obj</span> = json_decode(<span>$jsonStr</span><span>); </span><span>4</span> <span>echo</span> <span>$obj</span>->h;<span>//</span><span>使用成员访问的方式就可以得到结果</span>
知道简单的使用方法后,可以尝试着抓API数据,比如天气...