如何解码JSON字符串 [英] How to decode a JSON String

查看:101
本文介绍了如何解码JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家!我能否请您帮我解码此JSON代码:

everybody! Could I ask you to help me to decode this JSON code:

$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."}]}';

我想将上面的结构组织成这样:

I would like to organize above structure to this:

文件夹:收件箱

发件人:...

日期(日期):...

Date (date): ...

时间(时间):...

Time (time): ...

utcOffsetSeconds:...

utcOffsetSeconds: ...

收件人(地址):...

Recepient (address): ...

收件人(姓名):...

Recepient (name): ...

状态(deliveryStatus):...

Status (deliveryStatus): ...

文本(正文):...

Text (body): ...

...

提前谢谢!

推荐答案

您可以使用 json_decode 函数,以解码您的JSON字符串:

You can use the json_decode function, to decode your JSON string :

$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_decode($json);
var_dump($data);


然后您会得到像这样的东西:


And you'll get something like this :

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)
      ....
      ....


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


Now that you know the structure of the data, you can iterate over it ; for instance, you could use something like this :

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>';
}


这样您将获得以下输出:


And you'll get this kind of output :

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

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

...
...

这篇关于如何解码JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆