php 利用嵌套数组拼接与解析json
2016-12-20 19:15
633 查看
利用嵌套数组 拼接混合json -包含对象数组
php利用嵌套数组 解析混合json 包含对象数组
<?php // 自 PHP 5.4 起 $array = [ "status" => "0", "message" => "ok", "arr"=> [] ]; class Person { public $name; public $age; //定义一个构造方法初始化赋值 public function __construct($name,$age) { $this->name=$name; $this->age=$age; } } for($i=0;$i<10;$i++) { $p=new Person("ren",$i); $array["arr"][]=$p; } //var_dump($array); echo json_encode($array); ?>
php利用嵌套数组 解析混合json 包含对象数组
<?php function json_to_array($web) { $arr=array(); foreach($web as $k=>$v) { if(is_object($v)) $arr[$k]=json_to_array($v); //判断类型是不是object else $arr[$k]=$v; } return $arr; } $s='{"webname":"homehf","url":"www.homehf.com","qq":"744348666"}'; //将字符转成JSON $web=json_decode($s); $arr=array(); foreach($web as $k=>$v) $arr[$k]=$v; echo "<pre>"; print_r($arr); echo "</pre>"; $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}'; $web=json_decode($s); $arr=json_to_array($web); echo "<pre>"; print_r($arr); echo "</pre>"; /************************************************************************ ************************************************************************/ $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}'; $web=json_decode($s); echo '网站名称:'.$web->webname.'<br />网址:'.$web->url.'<br />联系方式:QQ-'.$web->contact->qq.' MAIL:'.$web->contact->mail; echo '<br /><br />'; /************************************************************************ ************************************************************************/ $s='{"webname":"homehf","url":"www.homehf.com","contact":{"qq":"744348666","mail":"nieweihf@163.com","xx":"xxxxxxx"}}'; $web=json_decode($s); echo json_encode($web); $mys='{"status":"0","message":"ok","arr":[{"name":"ren","age":0},{"name":"ren","age":1},{"name":"ren","age":2}, {"name":"ren","age":3},{"name":"ren","age":4},{"name":"ren","age":5},{"name":"ren","age":6},{"name":"ren","age":7}, {"name":"ren","age":8},{"name":"ren","age":9}]}'; $myweb=json_decode($mys); echo $myweb->status; for($i=0;$i<10;$i++) { echo $myweb->arr[$i]->age; echo '<br /><br />'; } ?>
相关文章推荐
- php利用嵌套数组拼接与解析json的方法
- php 利用json_decode强制json数据转换成数组
- PHP之-json转数组,支持多层嵌套json
- Android利用Gson解析嵌套多层的Json
- php 利用 json 传递数组之中文乱码最新解决办法
- Android利用Gson解析嵌套多层的Json
- 在CakePHP中利用Javascript Helper把PHP数组转换为JSON
- Android利用Gson解析嵌套多层的Json
- PHP 解析JSON数组
- 用org.json包解析嵌套数组json
- php使用js保存的json cookie 字符串,然后解析成数组,需要注意的事项
- PHP 解析JSON数组
- Android利用Gson解析嵌套多层的Json
- php解析json数组
- PHP解析Gson生成的Json数组
- Android利用Gson解析嵌套多层的Json
- String类型的json体转换为json(json嵌套字符串json,嵌套字符串json数组等)转换并解析为需要的json格式
- Android利用Gson解析嵌套多层的Json
- 利用Gson解析多层嵌套的JSON数据
- PHP利用 JSON 将XML转换为数组