0

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!

1
  • If you want to figure it out yourself try var_dump($json_data) or var_dump($json_data->ConsultarNfseResposta); in line 6 before the if statement and compare what you get with $json_data->ConsultarNfseResposta->ListaNfse->CompNfse step by step. Hope this is helpful... Commented Sep 2, 2017 at 20:17

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer JH, i removed the "true" and the error is gone... but now my echo is returning nothing. Any ideas what is going on? I've tried a few things, but no success.
1

@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

1 Comment

Nice find. ,$i++;) doesn't even throw an error. It just increments $i at the wrong time
0

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;
}

4 Comments

you need to change for to foreach in your code too! :)
Thank you. I made a mistake.
Thanks Ramazan and Wee Zel once again! You guys are amazing
no worries, someone would have downvoted when this is a good answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.