Read and interpret JSON files [duplicate]

1

I'm developing a program in which I need to read and interpret a JSON file, which is actually an XML file transformed into JSON. I need to select and get some information from this JSON file and save it in variables, such as note number, its value, client cnpj etc.

Below is the JSON file (I have edited some values).

{
    "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"
                            }
                        }
                    }
                }
            ]
        }
    }
}

In PHP PHP I'm able to decode the JSON file and throw it into a variable, as well as display the information on the screen with print_r in an organized way, but I've tried everything and I can not put a foreach to work, so I can get certain values in that file.

Follow my code in php:

<?php

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

$json_data = json_decode($json,true);

echo '<pre>' . print_r($json_data, true) . '</pre>';

//logica do foreach

?>
  

I put only one item, in fact it's almost 50.

    
asked by anonymous 01.09.2017 / 19:29

1 answer

2

What you need to understand is that when you decode a JSON file into a variable, this variable becomes an object, and like every object in PHP , you need to access each element through -> . To access the array first and iterate through it, you first need to access object by object until you reach the variable that represents the list that contains all other objects. The script below will probably work for you, you just need to complement the variables you want to access.

<?php

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

    $json_data = json_decode($json);

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

        echo $json_data->ConsultarNfseResposta->ListaNfse->CompNfse[$i]->Nfse->...
        echo $json_data->ConsultarNfseResposta->ListaNfse->CompNfse[$i]->PrestadorServico->...
        echo $json_data->ConsultarNfseResposta->ListaNfse->CompNfse[$i]->TomadorServico->...
        echo $json_data->ConsultarNfseResposta->ListaNfse->CompNfse[$i]->OrgaoGerador->...
    }

?>

Test the script and see if it works, if there is a problem, comment here that I help.

    
01.09.2017 / 22:55