zoukankan      html  css  js  c++  java
  • php操作json

    <meta charset='utf-8'>
    <?php
    //1、将索引数组转成json格式
    $stu=array('tom','berry','ketty','rose');
    $str=json_encode($stu);                   //将PHP数组转成JSON格式
    echo $str,'<br>';                       //["tom","berry","ketty","rose"]
    print_r(json_decode($str));              //Array ( [0] => tom [1] => berry [2] => ketty [3] => rose )
    echo '<hr>';
    //2、将关联数组转成json格式
    $stu=array('name'=>'tom','sex'=>'男','age'=>20);
    $str=json_encode($stu);                  //{"name":"tom","sex":"u7537","age":20}
    echo $str,'<br>';
    print_r(json_decode($str));              //stdClass Object ( [name] => tom [sex] => 男 [age] => 20 )
    echo '<hr>';
    //3、既有索引数组,又有关联数组
    $stu=array('name'=>'tom','berry','ketty');
    $str=json_encode($stu);                  //{"name":"tom","0":"berry","1":"ketty"}
    echo $str,'<br>';
    print_r(json_decode($str));              //stdClass Object ( [name] => tom [0] => berry [1] => ketty )
    echo '<hr>';
    //4、转换二维数组
    echo json_encode(array(
        array('name'=>'tom','sex'=>'M'),
        array('name'=>'berry','sex'=>'F')
        )),'<br>';                      //[{"name":"tom","sex":"M"},{"name":"berry","sex":"F"}]
    echo json_encode(array(
        'stu1'=>array('name'=>'tom','sex'=>'M'),
        'stu2'=>array('name'=>'berry','sex'=>'F')
        )),'<hr>';                      //{"stu1":{"name":"tom","sex":"M"},"stu2":{"name":"berry","sex":"F"}}
    //5、转换对象
    class Stu{
        public $name='tom';
        private $sex='M';
        public function getName(){}
    }
    $stu=new Stu();
    $str=json_encode($stu);    
    echo $str,'<br>';                     //{"name":"tom"}
    print_r(json_decode($str));              //stdClass Object ( [name] => tom )
    echo '<br>';
    print_r(json_decode($str,true));            //Array ( [name] => tom ),true表示反编译成数组 
    
  • 相关阅读:
    WPF编程系列
    使用ListBox控件来实现直方图控件(一)
    在WebBrowser控件中获取鼠标在网页上点击的位置
    使用WPF Resource以及Transform等技术实现鼠标控制图片缩放和移动的效果
    浮点数类型在计算机里面的表示方法
    点击asp:button按钮后,不刷新当前页面
    .NET ComponentArt 使用背景
    关于Microsoft ASP.NET 2.0 AJAX Extensions UpdatePanel 中使用 javascript 产生错误的问题
    基础平台数据导入(游标的使用)
    STUFF 函数的使用
  • 原文地址:https://www.cnblogs.com/aten/p/8643432.html
Copyright © 2011-2022 走看看