PHP对JSON数据格式的操作

JSON数据解码-json_decode:

<?php
// 定义一个数组变量
$fruit = array('north' => 'pear', 'shandong' => 'apple', 'hainan' => 'banana');
// 对此变量进行json编码
$json_fruit = json_encode($fruit);
//输出JSON信息
var_dump($json_fruit);
echo '<hr>';
// 输出对JSON信息反编码,并设置其返回值为array
var_dump(json_decode($json_fruit, true));

转码为JSON格式数据-json_encode:

<?php
// 关联数组
$season = array('one' => 'spring', 'two' => 'summer', 'three' => 'autumn', 'four' => 'winter');
echo json_encode($season);
echo '<hr>';
// 索引数组
$fruit = array('banana', 'apple', 'mango', 'orange');
echo json_encode($fruit);
echo '<hr>';
// 索引关联数组
$color = array('red', 'green', 'pink' => 'pink', 'purple');
echo json_encode($color);
echo '<hr>';
// 二维数组:一维是索引数组,二维是关联数组
$s1 = array('name' => 'job', 'addr' => 'America');
$s2 = array('name' => 'tom', 'addr' => 'China');
$student = array($s1, $s2);
echo json_encode($student);
echo '<hr>';
// 定义一个类
class Animal {
    public $species = 'breastfeeding';
    public $habits = 'feeding';
    public function run() {
        echo '正在跑。。。';
    }
}

$animal = new Animal;
echo json_encode($animal);

原文地址:https://blog.csdn.net/qq_42195688/article/details/80372568

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值