简体   繁体   English

如何解码JSON字符串

[英]How to decode a JSON String

everybody! 大家好! Could I ask you to help me to decode this JSON code: 我可以请你帮我解码这个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."}]}';

I would like to organize above structure to this: 我想组织以上结构:

Note 1: 注1:

Folder: inbox 文件夹:收件箱

From (from): ... 从(来自):......

Date (date): ... 日期(日期):......

Time (time): ... 时间(时间):......

utcOffsetSeconds: ... utcOffsetSeconds:...

Recepient (address): ... 受托人(地址):......

Recepient (name): ... 受托人(姓名):......

Status (deliveryStatus): ... 状态(deliveryStatus):...

Text (body): ... 文字(正文):...

Note 2: 笔记2:

... ...

Thank you in advance! 先感谢您!

You can use the json_decode function, to decode your JSON string : 您可以使用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);


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.

...
...

Looks like the recipients property is an array, try this: 看起来recipients属性是一个数组,试试这个:

$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 />';
        }
    }
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>";
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM