How to decode a JSON String

后端 未结 3 870
醉话见心
醉话见心 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:27

    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 '

    '; echo 'From : ' . htmlspecialchars($note->from) . '
    '; echo 'Date : ' . htmlspecialchars($note->date) . '
    '; echo 'Time : ' . htmlspecialchars($note->time) . '
    '; echo 'Body : ' . htmlspecialchars($note->body) . '
    '; foreach($note->recipients as $recipient) { echo 'To (address) : ' . htmlspecialchars($recipient->address) . '
    '; echo 'To (name) : ' . htmlspecialchars($recipient->name) . '
    '; echo 'Status : ' . htmlspecialchars($recipient->deliveryStatus) . '
    '; } }

提交回复
热议问题