Home > Article > Backend Development > PHP study notes 7-JSON data operation, php study notes 7-json_PHP tutorial
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 do the encode operation first:
<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>
The result is [1,2,3,4,5,6,7,8,9,"Hello","PHP"]
Do the decoding operation again:
<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>
After you know the simple usage method, you can try to capture API data, such as weather...