Fórum Como ler uma array dentro de outro array no json no delphi? #603337
28/06/2019
0
{
"trabalha_trabalhou": {
"existe_informacao": "SIM",
"conteudo": [
{
"documento": "5470777000100",
"nome": "SITIO PICAPAU AMARELA ",
"logradouro": "",
"endereco": "ESTADA BABABA",
"bairro": "QQREE",
"cidade": "MAMAM",
"numero": "5",
"cep": "",
"bloco": "",
"apartamento": "",
"casa": "",
"quadra": "",
"lote": "",
"complemento": "APTO. 01",
"uf": "RS",
"telefones": [
{
"ddd": "51",
"telefone": "00055210"
},
{
"ddd": "51",
"telefone": "00085217"
}
]
}
]
}
}Só postei uma parte do json, ele é muito maior que este ai, mais é o único com array dentro de outro array.
Todos faço mais ou menos assim que faz a leitura como quero
procedure TResideMesmoDomicilio.pLejsonDadosResideMesmoDomicilio(
const JSONObjectHeader: TJSONObject; TipoChaveJSON: String);
var
i: Integer;
begin
/// Dados Endereço
JSONObjectDadosResideMesmoDomicilio := JSONObjectHeader.GetValue(TipoChaveJSON)
as TJSONObject;
try
if JSONObjectDadosResideMesmoDomicilio <> nil then
begin
ArrayJsonDadosResideMesmoDomicilio :=
JSONObjectDadosResideMesmoDomicilio.GetValue('conteudo')
as TJSONArray;
if ArrayJsonDadosResideMesmoDomicilio <> nil then
begin
for i := 0 to Pred(ArrayJsonDadosResideMesmoDomicilio.Count) do
begin
CurrCondDadosResideMesmoDomicilio := ArrayJsonDadosResideMesmoDomicilio.Items[i]
as TJSONObject;
Fdados_pessoais_id := DM.Fdados_pessoais_id(cpf_cnpj_Pai);
Fcpf := Trim(CropStrRight(CurrCondDadosResideMesmoDomicilio.GetValue('documento').Value, 11));
DM.IDadosPessoais := Fdados_pessoais_id;
if DM.fConsultaCPF_CNPJ_Pessoa('cpf',
'procob_v2.circulo_pessoa_fisica', Fcpf, True) then
pUpdateDadosResideMesmoDomicilio;
end;
end;
end;
finally
FreeAndNil(JSONObjectDadosResideMesmoDomicilio);
end;
end;Só que este acima não consigo pegar o array do telefone
Qual a maneira de eu ler este array de telefones pelo delphi?
Adriano Dolce
Curtir tópico
+ 0Posts
28/06/2019
Rafael Bosco
ArrayJsonDadosTelefonesEndereco := JSONObjectDadosTelefonesEndereco.GetValue('telefones') as TJSONArray;
if ArrayJsonDadosTelefonesEndereco <> nil then
begin
for i := 0 to Pred(ArrayJsonDadosTelefonesEndereco.Count) do
begin
end;
end;
Coloque esse código dentro do FOR que você fez pra ler o endereço...
Gostei + 0
01/07/2019
Adriano Dolce
ArrayJsonDadosTelefonesEndereco := JSONObjectDadosTelefonesEndereco.GetValue('telefones') as TJSONArray;
if ArrayJsonDadosTelefonesEndereco <> nil then
begin
for i := 0 to Pred(ArrayJsonDadosTelefonesEndereco.Count) do
begin
end;
end;
Coloque esse código dentro do FOR que você fez pra ler o endereço...
Bom dia, eu até tentei fazer desta forma, mais gera um AV
procedure TDadosContatosTrabalho.pLejsonDadosContatosTrabalho
(const JSONObjectHeader: TJSONObject; TipoChaveJSON: String);
var
i, j: Integer;
begin
/// Retorna ID do Pai
Ftrabalho_id := DM.Fdados_pessoais_id(cnpj_Pai);
/// Dados Telefones
JSONObjectDadosContatosTrabalho := JSONObjectHeader.GetValue(TipoChaveJSON)
as TJSONObject;
try
if JSONObjectDadosContatosTrabalho <> nil then
begin
ArrayJsonDadosContatosTrabalho := JSONObjectDadosContatosTrabalho.GetValue
('conteudo') as TJSONArray;
if ArrayJsonDadosContatosTrabalho <> nil then
begin
for i := 0 to Pred(ArrayJsonDadosContatosTrabalho.Count) do
begin
CurrCondDadosContatosTrabalho := ArrayJsonDadosContatosTrabalho.Items
[i] as TJSONObject;
ArrayJsonDadosTelefonesEndereco :=
JSONObjectDadosTelefonesEndereco.GetValue('telefones')
as TJSONArray;
if ArrayJsonDadosTelefonesEndereco <> nil then
begin
for j := 0 to Pred(ArrayJsonDadosTelefonesEndereco.Count) do
begin
CurrCondTelefone := ArrayJsonDadosTelefonesEndereco.Items[j]
as TJSONObject;
try
Ftelefone :=
CropStrLeft(Trim(CurrCondTelefone.GetValue('telefones')
.Value), 12);
except
end;
end;
end;
// /// Grava
// pGravarDados_DadosTelefone;
end;
/// FOR
end; // FIM DO FIXO
end;
finally
FreeAndNil(JSONObjectDadosContatosTrabalho);
end;
end;
No call stack aparece isso
System.JSON.TJSONObject.GetPairByName('telefones')
System.JSON.TJSONObject.GetValue(???)
UnitContatosTrabalho.TDadosContatosTrabalho.pLejsonDadosContatosTrabalho($419CDA8,'trabalha_trabalhou')
Gostei + 0
01/07/2019
Adriano Dolce
ArrayJsonDadosTelefonesEndereco := JSONObjectDadosTelefonesEndereco.GetValue('telefones') as TJSONArray;
if ArrayJsonDadosTelefonesEndereco <> nil then
begin
for i := 0 to Pred(ArrayJsonDadosTelefonesEndereco.Count) do
begin
end;
end;
Coloque esse código dentro do FOR que você fez pra ler o endereço...
Bom dia, eu até tentei fazer desta forma, mais gera um AV
procedure TDadosContatosTrabalho.pLejsonDadosContatosTrabalho
(const JSONObjectHeader: TJSONObject; TipoChaveJSON: String);
var
i, j: Integer;
begin
/// Retorna ID do Pai
Ftrabalho_id := DM.Fdados_pessoais_id(cnpj_Pai);
/// Dados Telefones
JSONObjectDadosContatosTrabalho := JSONObjectHeader.GetValue(TipoChaveJSON)
as TJSONObject;
try
if JSONObjectDadosContatosTrabalho <> nil then
begin
ArrayJsonDadosContatosTrabalho := JSONObjectDadosContatosTrabalho.GetValue
('conteudo') as TJSONArray;
if ArrayJsonDadosContatosTrabalho <> nil then
begin
for i := 0 to Pred(ArrayJsonDadosContatosTrabalho.Count) do
begin
CurrCondDadosContatosTrabalho := ArrayJsonDadosContatosTrabalho.Items
[i] as TJSONObject;
ArrayJsonDadosTelefonesEndereco :=
JSONObjectDadosTelefonesEndereco.GetValue('telefones')
as TJSONArray;
if ArrayJsonDadosTelefonesEndereco <> nil then
begin
for j := 0 to Pred(ArrayJsonDadosTelefonesEndereco.Count) do
begin
CurrCondTelefone := ArrayJsonDadosTelefonesEndereco.Items[j]
as TJSONObject;
try
Ftelefone :=
CropStrLeft(Trim(CurrCondTelefone.GetValue('telefones')
.Value), 12);
except
end;
end;
end;
// /// Grava
// pGravarDados_DadosTelefone;
end;
/// FOR
end; // FIM DO FIXO
end;
finally
FreeAndNil(JSONObjectDadosContatosTrabalho);
end;
end;
No call stack aparece isso
System.JSON.TJSONObject.GetPairByName('telefones')
System.JSON.TJSONObject.GetValue(???)
UnitContatosTrabalho.TDadosContatosTrabalho.pLejsonDadosContatosTrabalho($419CDA8,'trabalha_trabalhou')
Ajustei aqui, e agora parece que resolveu, usei try/except pq alguns tava dando erro de typecast pq tava sem p cplchete no array.
Resultado final do codigo
procedure TDadosContatosTrabalho.pLejsonDadosContatosTrabalho
(const JSONObjectHeader: TJSONObject; TipoChaveJSON: String);
var
i, j: Integer;
begin
/// Retorna ID do Pai
Ftrabalho_id := DM.Fdados_pessoais_id(cnpj_Pai,
'procob_v2.trabalhos', 'cnpj');
/// Dados Telefones
JSONObjectDadosContatosTrabalho := JSONObjectHeader.GetValue(TipoChaveJSON)
as TJSONObject;
try
if JSONObjectDadosContatosTrabalho <> nil then
begin
ArrayJsonDadosContatosTrabalho := JSONObjectDadosContatosTrabalho.GetValue
('conteudo') as TJSONArray;
if ArrayJsonDadosContatosTrabalho <> nil then
begin
for i := 0 to Pred(ArrayJsonDadosContatosTrabalho.Count) do
begin
CurrCondDadosContatosTrabalho := ArrayJsonDadosContatosTrabalho.Items
[i] as TJSONObject;
ArrayJsonDadosTelefonesEndereco := nil;
try
ArrayJsonDadosTelefonesEndereco :=
CurrCondDadosContatosTrabalho.GetValue('telefones') as TJSONArray;
if ArrayJsonDadosTelefonesEndereco <> nil then
begin
for j := 0 to Pred(ArrayJsonDadosTelefonesEndereco.Count) do
begin
CurrCondTelefone := ArrayJsonDadosTelefonesEndereco.Items[j]
as TJSONObject;
try
Fddd :=
CropStrLeft(Trim(CurrCondTelefone.GetValue('ddd')
.Value), 12);
except
/// Pode ser que não exista no json ai exception silencioso
end;
try
Ftelefone :=
CropStrLeft(Trim(CurrCondTelefone.GetValue('telefone')
.Value), 12);
except
/// Pode ser que não exista no json ai exception silencioso
end;
Ftipo := 'Trabalho';
end;
end;
except
/// Pode ser que não exista ARRAY no json ai exception silencioso
end;
// /// Grava
pGravarDados_DadosTelefone;
end;
/// FOR
end; // FIM DO FIXO
end;
finally
FreeAndNil(JSONObjectDadosContatosTrabalho);
end;
end;Obrigado.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)