fjurr
fjurr

Reputation: 552

Error trying to get property of non-object from a JSON file with PHP

I am trying to get data with php from a JSON file. This is only one block of data from my JSON... in this JSON file, there is 50 more of so. Here it is:

{
"ConsultarNfseResposta": {
"ListaNfse": {
  "CompNfse": [
    {
      "Nfse": {
        "InfNfse": {
          "Numero": "12651",
          "CodigoVerificacao": "ECSV-EJZZ",
          "DataEmissao": "2017-07-25T17:51:12",
          "NaturezaOperacao": "1",
          "OptanteSimplesNacional": "1",
          "IncentivadorCultural": "2",
          "Competencia": "2017-07-25T00:00:00",
          "Servico": {
            "Valores": {
              "ValorServicos": "2350",
              "IssRetido": "2",
              "BaseCalculo": "2350",
              "Aliquota": "0.02",
              "ValorLiquidoNfse": "2350"
            },
            "ItemListaServico": "0107",
            "CodigoTributacaoMunicipio": "6209100",
            "Discriminacao": "TAXA: SERVIÇO DE VOTAÇÃO ELETRÔNICA",
            "CodigoMunicipio": "2611606"
          },
          "PrestadorServico": {
            "IdentificacaoPrestador": {
              "Cnpj": "41069964000173",
              "InscricaoMunicipal": "2427745"
            },
            "RazaoSocial": "INFORMATICA LTDA",
            "Endereco": {
              "Endereco": "RUA 241",
              "Numero": "241",
              "Bairro": "Exemplo",
              "CodigoMunicipio": "2611606",
              "Uf": "PE",
              "Cep": "52030190"
            },
            "Contato": {
              "Telefone": "33254854",
              "Email": "[email protected]"
            }
          },
          "TomadorServico": {
            "IdentificacaoTomador": {
              "CpfCnpj": {
                "Cnpj": "00085803000196"
              }
            },
            "RazaoSocial": "EXEMPLO - AMBR",
            "Endereco": {
              "Endereco": "ST 06",
              "Bairro": "Asa Sul",
              "CodigoMunicipio": "5300108",
              "Uf": "DF",
              "Cep": "15425845211"
            },
            "Contato": {
              "Email": "[email protected]"
            }
          },
          "OrgaoGerador": {
            "CodigoMunicipio": "2611606",
            "Uf": "PE"
          }
         }
       }
      }
    ]
   }
  }
 }

And here is my php code:

<?php

$json = file_get_contents('arquivo.json');

$json_data = json_decode($json,true);

for ($i=0; $i < count($json_data->ConsultarNfseResposta->ListaNfse->CompNfse), $i++;) {

    echo $json_data->ConsultarNfseResposta->ListaNfse->CompNfse[$i]->Nfse->InfNfse->Numero;
    echo $json_data->ConsultarNfseResposta->ListaNfse->CompNfse[$i]->Nfse->InfNfse->CodigoVerificacao;

}

?>

I am getting error at line 7: Notice: Trying to get property of non-object. I've already tried everything and my code isn't working.

What can i do? Thanks!

Upvotes: 0

Views: 267

Answers (3)

Cesur APAYDIN
Cesur APAYDIN

Reputation: 836

Using foreach:

$json_data = json_decode(file_get_contents('arquivo.json'));

foreach ($json_data->ConsultarNfseResposta->ListaNfse->CompNfse as $data) {
    echo $data->Nfse->InfNfse->Numero;
    echo $data->Nfse->InfNfse->CodigoVerificacao;
}

Upvotes: 0

Wee Zel
Wee Zel

Reputation: 1324

@FernandoJuriolli, your for statement isn't right, you have a comma in the wrong place - it should look like this:

for ($i=0; $i < count($json_data->ConsultarNfseResposta->ListaNfse->CompNfse); $i++) {

with the answer from @jh1711, this should work

Upvotes: 1

jh1711
jh1711

Reputation: 2328

The second parameter in your json_decode() call tells PHP to convert objects to arrays. See the manual. You should be fine if you just remove the true.

Upvotes: 1

Related Questions