<?php
(1) //一维数组转换json
$array_1=array();//定义一维数据
$array_1[‘username‘]="hanxin";
$array_1[‘age‘]=18;
$jsonObj_1=json_encode($array_1);
//echo $jsonObj_1;
(2) //多维数组转换json
$array_2=array();//定义多维数组
$array_2[‘member‘][‘huiyuan1‘][‘username‘]="hanxin";
$array_2[‘member‘][‘huiyuan1‘][‘age‘]=18;
$array_2[‘member‘][‘huiyuan2‘][‘username‘]="hanxin2";
$array_2[‘member‘][‘huiyuan2‘][‘age‘]=19;
$jsonObj_2=json_encode($array_2);
//echo $jsonObj_2;
(3) //对象转换json
class hanxin{
public $name="public name";
protected $ptName="protected name";
private $pName="private name";
public function getName(){
return $this->name;
}
}
$hanxinObj=new hanxin();
$ObjJson=json_encode($mukeObj);
//print_r($ObjJson);
(4) //json 转换对象
$jsonStr=‘{"key":"value","key2":"value2"}‘;
$jsonStrToArray=json_decode($jsonStr);
//print_r($jsonStrToArray);
(5) //json 转换成数组
$jsonStr=‘{"key":"value","key2":"value2"}‘;
$jsonStrToArray=json_decode($jsonStr,true);
//print_r($jsonStrToArray);