如何解码JSON字符串

4
大家好!我能请你帮我解码这个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."}]}';

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

注意1:

文件夹:收件箱

发件人(from):...

日期(date):...

时间(time):...

utcOffsetSeconds:...

收件人地址(address):...

收件人姓名(name):...

状态(deliveryStatus):...

正文(body):...

注意2:

...

提前感谢您!

3个回答

11

您可以使用json_decode函数解码您的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_decode($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 : 55512351
Date : 29/03/2010
Body : This is message text.

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

...
...

Pascal MARTIN,感谢您的快速反应!但是问题出在这个多重数组上。我真的无法操作所有这些数组,并像我想要的那样制作一个简单的结构,就像分离的注释一样!请问您能否展示如何操作所有这些数组?您可以使用foreach()或其他方法展示吗?谢谢! - ilnur777
1
@ilnur777:如果你不知道foreach,请阅读文档:http://php.net/manual/en/control-structures.foreach.php。在处理数组时,`for`和`foreach`循环是必不可少的工具。 - Felix Kling
Pascal MARTIN,你救了我的命和神经!!!非常感谢你。我现在真的明白如何操作JSON数组了! - ilnur777
1
@ilnur777:别忘了将Pascal的答案标记为正确答案 :-) - Andy E
如果始终只有一个收件人,类似 $note->recipients[0]->name 的东西应该可以工作;如果有时不止一个,您可以使用 foreach 循环来迭代 $note->recipients - Pascal MARTIN

1

看起来收件人属性是一个数组,试试这个:

$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);
print_r($data);

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

        foreach($note->recipients as $recipient)
        {
            echo 'To (address) : ' . htmlspecialchars($recipient->address) . '<br />';
            echo 'To (name)    : ' . htmlspecialchars($recipient->name) . '<br />';
            echo 'Status       : ' . htmlspecialchars($recipient->deliveryStatus) . '<br />';
        }
    }


0
enter code here
<?php
$subject = file_get_contents('http://example.com/subject.php');
$subject_arr = json_decode($subject);

$my = $subject_arr->info;

for($i=0;$i<=1000;$i++){
echo $my[$i]->subject_name;
echo "<br>";
}
echo $my[0]->subject_name;

?>
<?php
$subject = file_get_contents('http://example.com/subject.php');
$subject_arr = json_decode($subject);

$my = $subject_arr->info;

for($i=0;$i<=1000;$i++){
echo $my[$i]->subject_name;
echo "<br>";
}
?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接