How to decode a JSON String

后端 未结 3 872
醉话见心
醉话见心 2020-11-28 15:01

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\         


        
3条回答
  •  孤城傲影
    2020-11-28 15:40

    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 '

    '; echo 'From : ' . htmlspecialchars($note->from) . '
    '; echo 'Date : ' . htmlspecialchars($note->date) . '
    '; echo 'Body : ' . htmlspecialchars($note->body) . '
    '; echo '

    '; }


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

提交回复
热议问题