Как декодировать строку JSON - PullRequest
4 голосов
/ 30 марта 2010

все! Могу ли я попросить вас помочь мне расшифровать этот код 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:

Папка: Входящие

От (с): ...

Дата (дата): ...

Время (время): ...

utcOffsetSeconds: ...

Получатель (адрес): ...

Получатель (имя): ...

Статус (deliveryStatus): ...

Текст (тело): ...

Примечание 2:

...

Заранее спасибо!

Ответы [ 3 ]

11 голосов
/ 30 марта 2010

Вы можете использовать функцию 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.

...
...
1 голос
/ 30 марта 2010

Похоже, свойство получателей является массивом, попробуйте это:

$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 голосов
/ 21 ноября 2016
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>";
}
?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...