0

I'm having a problem getting values from a JSON object. The script is working normally using another JSON structure, however with this specific JSON file, the error is happening. I believe the error is in "" content ": [" at the beginning of JSON.

I am using the following script:

<?php
$json_strdoc = file_get_contents("http://app.tce.ma.gov.br:8889/contratos/documentos?numeroContrato=03.004.0410");

    $objdoc = json_decode($json_strdoc);

   echo "
   <table class='table tabela-nova'>
    <thead>
        <tr>
            <th scope='col' style='width: 28%;'>Tipo de Documento</th>
            <th scope='col' style='width: 35.5%;'>Descrição</th>
            <th scope='col'>Data de Envio</th>
            <th scope='col'>Baixar</th>
        </tr>
    </thead>
    <tbody>";


        foreach ($objdoc as $itemdoc){

            echo "
    <tr>
            <td>".$itemdoc->content->documentos->tipoDocumento->nome."</td>
            <td>".$itemdoc->content->documentos->assunto."</td>
            <td>".$itemdoc->content->documentos->dataEnvio."</td>
            <td><span class='baixar'><i class='fa fa-angle-down'></i></span><a class='link-baixar' target='_blank' href='$itemdoc->content->link'> Baixar arquivo </a></td>
    </tr>
     " ;
        }
   echo "
    </tbody>
   </table>";

?>

However, the error is returning:

Notice: Trying to get property of non-object in...

enter image description here

The result should look like this:

enter image description here

  • 1
    Look at the structure of `$objdoc` before you try to use it. It has pagination details on the main object, and the information you're looking for is inside of `$objdoc->content`. You'll want to iterate through `$objdoc->content` instead of `$objdoc` – aynber Jan 29 '20 at 15:12
  • Thanks for responding, @Aynber . I need to get the values that are inside `" "content": ["` in the requested JSON file. What should I do? I'm using a translator, sorry. – Alexandre Lopes Jan 29 '20 at 15:42
  • 1
    Like I said, you need to use `$objdoc->content` instead of `$objdoc` on your foreach, and then you'll need to remove `content` from the object properties inside of your foreach. Looking more at the details, `content` contains 1 array with a key of 0 and you apparently really want the documents. So instead, it would be `foreach($objdoc->content[0]->documentos as $itemdoc)`, then you'd access the properties with `$itemdoc->tipoDocumento->nome`, `$itemdoc->assunto `, and `$itemdoc->dataEnvio`. When trying to figure out the data structure, `var_dump` is your friend. – aynber Jan 29 '20 at 15:46
  • Wow, thank you so much! That was what I needed. Thank you so much brother! – Alexandre Lopes Jan 29 '20 at 15:53
  • 1
    Um... not a guy. But you're welcome! – aynber Jan 29 '20 at 15:54
  • Sorry, haha. Thank you. – Alexandre Lopes Jan 29 '20 at 15:55

0 Answers0