php 解析Json

助手阿喜 使用手册评论181字数 723阅读2分24秒阅读模式

 

json_decode()函数:函数原型json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] ),当$assoc为true时将返回的是 array 而非 object ,什么意思呢,直接看例子:文章源自助手阿喜-https://zsaxi.com/4805

1、json格式1文章源自助手阿喜-https://zsaxi.com/4805

常见的json格式:$json = {"state": "1","test": "test"}文章源自助手阿喜-https://zsaxi.com/4805

解析这个json可以下面两种方式:文章源自助手阿喜-https://zsaxi.com/4805

若是$de_json = json_decode($json);//变成了对象,输出时候应该这样获取值:echo $de_json ->state;文章源自助手阿喜-https://zsaxi.com/4805

若是$de_json = json_decode($json,true);  //变成数组了,输出时候应该这样获取值:echo $de_json['state']; 文章源自助手阿喜-https://zsaxi.com/4805

2、json格式2 object文章源自助手阿喜-https://zsaxi.com/4805

$json = {"state": "1",  "list": [{ "IODataID": "154036",  "DataDate": "20131213",  "DataTime": "071824" }, { "IODataID": "154042", "DataDate": "20131213",  "DataTime": "071835"}]}文章源自助手阿喜-https://zsaxi.com/4805

解析这样的json:文章源自助手阿喜-https://zsaxi.com/4805

对于获取state都是一样的,没什么变化,但是里面有包含了数组,这个时候文章源自助手阿喜-https://zsaxi.com/4805

如果是:$de_json = json_decode($json)文章源自助手阿喜-https://zsaxi.com/4805

获取$list = $de_json->list 这样后取得的是一个对象数组,在进行进一步获取foreach遍历($list[0]->IODataID //获得154036)文章源自助手阿喜-https://zsaxi.com/4805 文章源自助手阿喜-https://zsaxi.com/4805

  • 扫码入群
  • 加群获取附件资源,请查看教程说的资源名称查找。
  • weinxin
  • 支持打赏
  • 如果觉得本文对你有帮助,可以打赏任意金额已维持网站运行。
  • weinxin
助手阿喜
  • 本文由 发表于 2021年12月8日 15:24:36