json数据格式的相互转换

导语 <?php$array_1=array(); 一维数组$array_2=array(); 二维数组$array_1[& 39;username& 39;]="Aseoe";$array_1[& 39;website& 39;]="http: www aseoe com ";$array_2[& 39;member& 39;][& 39;ancto net& 39;][

<?php 
  1.  
  2.     $array_1 = array();  //一维数组 
  3.     $array_2 = array();  //二维数组 
  4.  
  5.     $array_1['username'] = "Aseoe"
  6.     $array_1['website'] = "https://www.nucmc.com/"
  7.  
  8.     $array_2['member']['ancto.net']['username'] = "ancto"
  9.     $array_2['member']['ancto.net']['website'] = "https://www.ancto.net/"
  10.  
  11.     $array_2['member']['nucmc.com']['username'] = "aseoe"
  12.     $array_2['member']['nucmc.com']['website'] = "https://www.nucmc.com/"
  13.  
  14.     //print_r($array_2); 
  15.  
  16.     $jsonObj_1 = json_encode($array_1);  //一维数组转换成json数据格式 
  17.     //echo $jsonObj_1; 
  18.  
  19.     $jsonObj_2 = json_encode($array_2);     //多维数组转换成json数据格式 
  20.     //echo $jsonObj_2; 
  21.  
  22.     class aseoe{ 
  23.         public $puname = "public name"
  24.         protected $poname = "protected name"
  25.         private $prname = "private name"
  26.  
  27.         public function getName(){ 
  28.             return $this->name; 
  29.         } 
  30.  
  31.     } 
  32.  
  33.     $aseoeObj = new aseoe(); 
  34.     $json_aseoe =json_encode($aseoeObj);    //对象转换成json数据格式 
  35.     //echo $json_aseoe;    //对象转换为json数据时,只转换公有变量,私有变量不转换 
  36.  
  37.     //print_r($aseoeObj); 
  38.  
  39.     $jsonStr = '{"key":"value","key1":"value2"}'
  40.     $json2Array = json_decode($jsonStr,true);  //没设第2个参数时,默认是对象类型 
  41.     print_r($json2Array); 

 

$jsonStr='{"key":"value","key1":"value1"}'; 
 
json_decode($jsonStr);//转换之后是对象类型 
 
json_decode($jsonStr,true);//转换之后是数组 

 


 

https://www.nucmc.com/ true json数据格式的相互转换 https://www.nucmc.com/show-12-467-1.html report 3201 <?php$array_1=array(); 一维数组$array_2=array(); 二维数组$array_1[& 39;username& 39;]="Aseoe";$array_1[& 39;website& 39;]="http: www aseoe com ";$array_2[& 39;member& 39;][& 39;ancto net& 39;][
TAG:json 格式数据
本站欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明: 文章转载自:BETWAY官网网 https://www.nucmc.com/show-12-467-1.html
BETWAY官网网 Copyright 2012-2014 www.nucmc.com All rights reserved.(晋ICP备13001436号-1)