$array_1 = array(); //一维数组
$array_2 = array(); //二维数组
$array_1[‘username‘] = "Aseoe";
$array_1[‘website‘] = "http://www.aseoe.com/";
$array_2[‘member‘][‘ancto.net‘][‘username‘] = "ancto";
$array_2[‘member‘][‘ancto.net‘][‘website‘] = "http://www.ancto.net/";
$array_2[‘member‘][‘aseoe.com‘][‘username‘] = "aseoe";
$array_2[‘member‘][‘aseoe.com‘][‘website‘] = "http://www.aseoe.com/";
//print_r($array_2);
$jsonObj_1 = json_encode($array_1); //一维数组转换成json数据格式
//echo $jsonObj_1;
$jsonObj_2 = json_encode($array_2); //多维数组转换成json数据格式
//echo $jsonObj_2;
class aseoe{
public $puname = "public name";
protected $poname = "protected name";
private $prname = "private name";
public function getName(){
return $this->name;
}
}
$aseoeObj = new aseoe();
$json_aseoe =json_encode($aseoeObj); //对象转换成json数据格式
//echo $json_aseoe; //对象转换为json数据时,只转换公有变量,私有变量不转换
//print_r($aseoeObj);
$jsonStr = ‘{"key":"value","key1":"value2"}‘;
$json2Array = json_decode($jsonStr,true); //没设第2个参数时,默认是对象类型
print_r($json2Array);
$jsonStr=‘{"key":"value","key1":"value1"}‘; json_decode($jsonStr);//转换之后是对象类型 json_decode($jsonStr,true);//转换之后是数组
原文:http://www.cnblogs.com/yangjinfeng172/p/4986435.html