4

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:

Note 1:

Folder: inbox

From (from): ...

Date (date): ...

Time (time): ...

utcOffsetSeconds: ...

Recepient (address): ...

Recepient (name): ...

Status (deliveryStatus): ...

Text (body): ...

Note 2:

...

Thank you in advance!

Gordon
  • 312,688
  • 75
  • 539
  • 559
ilnur777
  • 1,135
  • 5
  • 16
  • 28

3 Answers3

11

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.

...
...
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • Pascal MARTIN, thank you for quick react! But the problem in this multiply arrays. I really can't operate with all these arrays and make a simple structure as I would like to do it above like separate notes! Please, I ask you could you show how should I operate with all these arrays? Could you show it using I don't know foreach() or what? Thank you! – ilnur777 Mar 30 '10 at 07:14
  • 1
    @ilnur777: If you don't know `foreach`, read the documentation: http://php.net/manual/en/control-structures.foreach.php . `for` and `foreach` loops are essential tools when dealing with arrays. – Felix Kling Mar 30 '10 at 07:19
  • Pascal MARTIN, you saved my life and nerves!!! Thank you very much. It really now understandable for me how to operate with arrays on JSON! – ilnur777 Mar 30 '10 at 07:20
  • 1
    @ilnur777: Don't forget to mark Pascal's answer as the correct one :-) – Andy E Mar 30 '10 at 07:25
  • If there is always one and only one recipient, something like `$note->recipients[0]->name` should work ; if there is sometimes more than one, you can use a `foreach` loop to iterate over `$note->recipients` – Pascal MARTIN Mar 30 '10 at 10:23
1

Looks like the recipients property is an array, try this:

$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 />';
        }
    }
deadkarma
  • 3,144
  • 2
  • 17
  • 19
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>";
}
?>
Siddharth Shukla
  • 1,063
  • 15
  • 14