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.