我正在尝试从JSON文件中获取PHP数据。 这只是我JSON中的一个数据块...在这个JSON文件中,还有50个以上。 这是:
{
"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": "exemplo@exemplo.com.br"
}
},
"TomadorServico": {
"IdentificacaoTomador": {
"CpfCnpj": {
"Cnpj": "00085803000196"
}
},
"RazaoSocial": "EXEMPLO - AMBR",
"Endereco": {
"Endereco": "ST 06",
"Bairro": "Asa Sul",
"CodigoMunicipio": "5300108",
"Uf": "DF",
"Cep": "15425845211"
},
"Contato": {
"Email": "exemplo@gmail.com"
}
},
"OrgaoGerador": {
"CodigoMunicipio": "2611606",
"Uf": "PE"
}
}
}
}
]
}
}
}
这是我的PHP代码:
<?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;
}
?>
我在第7行遇到错误: 注意:尝试获取非对象的属性。我已经尝试了所有内容,而且我的代码也没有用。
我该怎么办? 谢谢!
答案 0 :(得分:1)
json_decode()
调用中的第二个参数告诉PHP将对象转换为数组。见the manual。如果您删除true
。
答案 1 :(得分:1)
@FernandoJuriolli,你的for声明不对,你在错误的地方有一个逗号 - 它应该是这样的:
for ($i=0; $i < count($json_data->ConsultarNfseResposta->ListaNfse->CompNfse); $i++) {
来自@ jh1711的答案,这应该有效
答案 2 :(得分:0)
使用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;
}