php的json数据包编码和解析

一 知识点
01 php关联数组
   $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

   foreach($age as $x => $x_value)
   {
       echo "键名".$x." 键值".$x_value;
   }
02 php的json_ecode函数
   phase:
   string json_encode ( $value [, $options = 0 ] )
   ex:
   $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
   echo json_encode($arr);

03 php的json_decode函数
   phase:
   mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])
   ex:
   $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
   json_decode($json,true);    // true,解码为数组;false解码为对象
二 实践
  
  // 解析微信公众号的json数据包格式;
  $json_string = '{"access_token":"ACCESS_TOKEN","expires_in":7200}';

  $data = json_decode($json_string,true);

  var_dump($data);

  // 数组编码为json数据包;
  $arrTemp = array("access_token" => "ACCESS_TOKEN","expires_in"=> 7200);

  $json_data = json_encode($arrTemp);

  var_dump($json_data);

?>

你可能感兴趣的:(php的json数据包编码和解析)