php中JSON的使用方法 |
本文标签:php,json 从5.2版本开始,PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码 。 复制代码 代码如下: $arr = array (a=>a,b=>b,c=c,d=>d,e=e); echo json_encode($arr); 输出结果: class person { public $name; public $age; public $height; function __construct($name,$age,$height) { $this->name = $name; $this->age = $age; $this->height = $height; } } $obj = new person("zhangsan",20,100); $foo_json = json_encode($obj); echo $foo_json; 输出结果: 复制代码 代码如下: $json = {"a":"hello","b":"world","c":"zhangsan","d":20,"e":170}; var_dump(json_decode($json)); 输出结果: 复制代码 代码如下: $json = {"a":"hello","b":"world","c":"zhangsan","d":20,"e":170}; var_dump(json_decode($json,ture)); 以上所述就是本文的全部内容了,希望大家能够喜欢 。 |