Error while capturing JSON field?

Asked

Viewed 162 times

3

I’m making a mistake when I’m trying to reclaim a field of one array.

Follows the json:

{
      "data": {
        "messages": [
          {
            "id": 1,
            "sender": "[email protected]",
            "recipient": "[email protected]",
            "sent_at": "2015-01-22T18:17:53.586-02:00",
            "status": "delivered",
            "bounce_code": null,
            "subject": "teste"
          },
          {
            "id": 2,
            "sender": "[email protected]",
            "recipient": "[email protected]",
            "sent_at": "2015-01-22T18:17:53.686-02:00",
            "status": "bounced",
            "bounce_code": "5.1.1",
            "subject": "test2"
          }
        ]
      },
      "links": {
        "self": "http://api.smtplw.locaweb.com.br/v1/message_reports?end_date=2015-04-10&page=2&per=2&start_date=2015-01-01&status=all",
        "next": "http://api.smtplw.locaweb.com.br/v1/message_reports?end_date=2015-04-10&page=3&per=2&start_date=2015-01-01&status=all",
        "prev": null,
        "first": "http://api.smtplw.locaweb.com.br/v1/message_reports?end_date=2015-04-10&page=1&per=2&start_date=2015-01-01&status=all",
        "last": "http://api.smtplw.locaweb.com.br/v1/message_reports?end_date=2015-04-10&page=5&per=2&start_date=2015-01-01\u0026status=all"
      }
    }

My PHP is like this:

$jsonObj = json_decode($VARIAVEL_COM_O_JSON);
$resposta = $jsonObj->data;

foreach ($resposta as $c) {
    echo "$c->recipient<br>"; 
}

The error that occurs:

Notice: Trying to get property of non-object in /Library/WebServer/Documents/teste.php on line 24

Error line:

echo "$c->recipient<br>"; 

I need to write all the fields recipient of array?

  • 1

    You are doing as if recipient is within date, it is within messages

2 answers

6


On your foreach puts so:

foreach ($resposta->messages as $c) {
    echo "$c->recipient<br>"; 
}

What was happening is that your foreach was only passing the index date, so the first round always returned the array that was assigned to the index messages, so it will loop with this array.

  • Okay it worked out, thank you very much

  • 1

    Hello @Hugoborges, if the answer solved your problem, mark it as a solution, so that other people can also be helped.

  • fin friend a test here, on my local server is working but on the production server of wrong. I updated the question, have you help me?

  • @Hugoborges Of course, friend, the new mistake is giving when he tries to call the property data, probably its variable $resposta is arriving empty or with different format than you expect, but the ideal would be you create another issue to solve this, as it has nothing to do with the previous problem!

  • the strange and that is the same code. but I will create another question

  • Yeah, but it’s not about the code, it’s about before you get there, because probably this variable that you give a json_decode is coming empty, understood?

  • @Hugoborges creates another question and sends me the link so I can help you!

  • ok, vlw. follow the link http://answall.com/questions/161921/problemas-as-recuperar-objeto-de-um-json

Show 3 more comments

3

Your Json has several levels here you only enter level 1

$resposta = $jsonObj->data; 

within it has messages andlinks

you need to say the next level to make iteration

foreach ($resposta->messages as $c) {
    echo "$c->recipient<br>"; 
}
  • fin friend a test here, on my local server is working but on the production server of wrong. I updated the question, have you help me?

  • It does not recognize as an object, you can write with a var_dump($reply) and see if it says if it is an object, if you can’t, try as an array. no put true in the second parameter. json_decode($VARIAVEL_COM_O_JSON,true); and then try, $c["recipient"].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.