栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何解码JSON字符串

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何解码JSON字符串

您可以使用该

json_depre
函数来解码JSON字符串:

$json = '{"inbox":[{"from":"55512351","date":"29/03/2010","time":"21:24:10","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29/03/2010","time":"21:24:12","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29/03/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29/03/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."}]}';$data = json_depre($json);var_dump($data);

而且您会得到像这样的东西:

object(stdClass)[1]  public 'inbox' =>     array      0 =>         object(stdClass)[2]          public 'from' => string '55512351' (length=8)          public 'date' => string '29/03/2010' (length=10)          public 'time' => string '21:24:10' (length=8)          public 'utcOffsetSeconds' => int 3600          public 'recipients' =>  array   0 =>      object(stdClass)[3]       public 'address' => string '55512351' (length=8)       public 'name' => string '55512351' (length=8)       public 'deliveryStatus' => string 'notRequested' (length=12)          public 'body' => string 'This is message text.' (length=21)      1 =>         object(stdClass)[4]          public 'from' => string '55512351' (length=8)          public 'date' => string '29/03/2010' (length=10)          public 'time' => string '21:24:12' (length=8)          public 'utcOffsetSeconds' => int 3600          public 'recipients' =>  array   0 =>      object(stdClass)[5]       public 'address' => string '55512351' (length=8)       public 'name' => string '55512351' (length=8)       public 'deliveryStatus' => string 'notRequested' (length=12)          public 'body' => string 'This is message text.' (length=21)      ....      ....

既然您知道了数据的结构,就可以对其进行遍历;例如,您可以使用以下内容:

foreach ($data->inbox as $note) {  echo '<p>';  echo 'From : ' . htmlspecialchars($note->from) . '<br />';  echo 'Date : ' . htmlspecialchars($note->date) . '<br />';  echo 'Body : ' . htmlspecialchars($note->body) . '<br />';  echo '</p>';}

这样您将获得以下输出:

From : 55512351Date : 29/03/2010Body : This is message text.From : 55512351Date : 29/03/2010Body : This is message text.......


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402662.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号