Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Loading and Parsing a Complex JSON Array
See more JSON Examples
This example loads a JSON array containing more complex data. It shows how to parse (access) various values contained within the JSON.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.JsonArray,
Chilkat.StringBuilder,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sb: TStringBuilder;
bCrlf: Boolean;
jsonArray: TJsonArray;
numRecords: Integer;
i: Integer;
jsonRecord: TJsonObject;
numTelefones: Integer;
j: Integer;
numContatos: Integer;
k: Integer;
numEmails: Integer;
begin
success := False;
// This is the JSON we'll be loading:
// [
// {
// "telefones": [
// {
// "numero": "19995555555",
// "tipo": "T",
// "id": 2541437
// }
// ],
// "cnpj": "11395551000164",
// "rua": "R XAVIER AUGUSTO ROGGE, 22",
// "complemento": "",
// "contatos": [
// ],
// "tipo": "J",
// "razao_social": "SOUP BRASIL LTDA - ME",
// "nome_fantasia": "SOUP BRASIL",
// "bairro": "ABC DOS COLIBRIS",
// "cidade": "TEST",
// "inscricao_estadual": "222.102.222.116",
// "observacao": "",
// "id": 2209595,
// "ultima_alteracao": "2016-12-26 16:22:34",
// "cep": "13555000",
// "suframa": "",
// "estado": "SP",
// "emails": [
// {
// "email": "somebody@terra.com.br",
// "tipo": "T",
// "id": 1065557
// }
// ],
// "excluido": false
// },
// {
// "telefones": [
// ],
// "cnpj": "12496555500180",
// "rua": "AV ROLF WIEST, 100",
// "complemento": "ANDAR 7 SALA 612 A 620",
// "contatos": [
// ],
// "tipo": "J",
// "razao_social": "SIMPLE SOFTWARE LTDA",
// "nome_fantasia": "",
// "bairro": "DOM ZETIRO",
// "cidade": "APARTVILLE",
// "inscricao_estadual": "",
// "observacao": "",
// "id": 2255594,
// "ultima_alteracao": "2016-12-26 16:28:31",
// "cep": "89255505",
// "suframa": "",
// "estado": "SC",
// "emails": [
// ],
// "excluido": false
// },
// {
// "telefones": [
// {
// "numero": "1938655556",
// "tipo": "T",
// "id": 2555438
// }
// ],
// "cnpj": "00003555500153",
// "rua": "AV ABCDEF PINTO CATAO, 18",
// "complemento": "",
// "contatos": [
// {
// "telefones": [
// {
// "numero": "1999655554",
// "tipo": "T",
// "id": 2555559
// }
// ],
// "cargo": "zzz de compras",
// "nome": "Gerard",
// "emails": [
// {
// "email": "gerard@terra.com.br",
// "tipo": "T",
// "id": 1065559
// }
// ],
// "id": 844485,
// "excluido": false
// }
// ],
// "tipo": "J",
// "razao_social": "TIDY TECNOLOGIA LTDA - EPP",
// "nome_fantasia": "TIDY",
// "bairro": "TUNA",
// "cidade": "JAGUAR",
// "inscricao_estadual": "395.222.441.222",
// "observacao": "ligar sempre depois das 14hs",
// "id": 2255597,
// "ultima_alteracao": "2016-12-28 07:31:52",
// "cep": "13555500",
// "suframa": "",
// "estado": "SP",
// "emails": [
// {
// "email": "xi@tidy.com.br",
// "tipo": "T",
// "id": 10655558
// }
// ],
// "excluido": false
// }
// ]
//
// Construct a StringBuilder containing the above JSON array.
sb := TStringBuilder.Create;
bCrlf := True;
sb.AppendLine('[',bCrlf);
sb.AppendLine(' {',bCrlf);
sb.AppendLine(' "telefones": [',bCrlf);
sb.AppendLine(' {',bCrlf);
sb.AppendLine(' "numero": "19995555555",',bCrlf);
sb.AppendLine(' "tipo": "T",',bCrlf);
sb.AppendLine(' "id": 2541437',bCrlf);
sb.AppendLine(' }',bCrlf);
sb.AppendLine(' ],',bCrlf);
sb.AppendLine(' "cnpj": "11395551000164",',bCrlf);
sb.AppendLine(' "rua": "R XAVIER AUGUSTO ROGGE, 22",',bCrlf);
sb.AppendLine(' "complemento": "",',bCrlf);
sb.AppendLine(' "contatos": [',bCrlf);
sb.AppendLine(' ],',bCrlf);
sb.AppendLine(' "tipo": "J",',bCrlf);
sb.AppendLine(' "razao_social": "SOUP BRASIL LTDA - ME",',bCrlf);
sb.AppendLine(' "nome_fantasia": "SOUP BRASIL",',bCrlf);
sb.AppendLine(' "bairro": "ABC DOS COLIBRIS",',bCrlf);
sb.AppendLine(' "cidade": "TEST",',bCrlf);
sb.AppendLine(' "inscricao_estadual": "222.102.222.116",',bCrlf);
sb.AppendLine(' "observacao": "",',bCrlf);
sb.AppendLine(' "id": 2209595,',bCrlf);
sb.AppendLine(' "ultima_alteracao": "2016-12-26 16:22:34",',bCrlf);
sb.AppendLine(' "cep": "13555000",',bCrlf);
sb.AppendLine(' "suframa": "",',bCrlf);
sb.AppendLine(' "estado": "SP",',bCrlf);
sb.AppendLine(' "emails": [',bCrlf);
sb.AppendLine(' {',bCrlf);
sb.AppendLine(' "email": "somebody@terra.com.br",',bCrlf);
sb.AppendLine(' "tipo": "T",',bCrlf);
sb.AppendLine(' "id": 1065557',bCrlf);
sb.AppendLine(' }',bCrlf);
sb.AppendLine(' ],',bCrlf);
sb.AppendLine(' "excluido": false',bCrlf);
sb.AppendLine(' },',bCrlf);
sb.AppendLine(' {',bCrlf);
sb.AppendLine(' "telefones": [',bCrlf);
sb.AppendLine(' ],',bCrlf);
sb.AppendLine(' "cnpj": "12496555500180",',bCrlf);
sb.AppendLine(' "rua": "AV ROLF WIEST, 100",',bCrlf);
sb.AppendLine(' "complemento": "ANDAR 7 SALA 612 A 620",',bCrlf);
sb.AppendLine(' "contatos": [',bCrlf);
sb.AppendLine(' ],',bCrlf);
sb.AppendLine(' "tipo": "J",',bCrlf);
sb.AppendLine(' "razao_social": "SIMPLE SOFTWARE LTDA",',bCrlf);
sb.AppendLine(' "nome_fantasia": "",',bCrlf);
sb.AppendLine(' "bairro": "DOM ZETIRO",',bCrlf);
sb.AppendLine(' "cidade": "APARTVILLE",',bCrlf);
sb.AppendLine(' "inscricao_estadual": "",',bCrlf);
sb.AppendLine(' "observacao": "",',bCrlf);
sb.AppendLine(' "id": 2255594,',bCrlf);
sb.AppendLine(' "ultima_alteracao": "2016-12-26 16:28:31",',bCrlf);
sb.AppendLine(' "cep": "89255505",',bCrlf);
sb.AppendLine(' "suframa": "",',bCrlf);
sb.AppendLine(' "estado": "SC",',bCrlf);
sb.AppendLine(' "emails": [',bCrlf);
sb.AppendLine(' ],',bCrlf);
sb.AppendLine(' "excluido": false',bCrlf);
sb.AppendLine(' },',bCrlf);
sb.AppendLine(' {',bCrlf);
sb.AppendLine(' "telefones": [',bCrlf);
sb.AppendLine(' {',bCrlf);
sb.AppendLine(' "numero": "1938655556",',bCrlf);
sb.AppendLine(' "tipo": "T",',bCrlf);
sb.AppendLine(' "id": 2555438',bCrlf);
sb.AppendLine(' }',bCrlf);
sb.AppendLine(' ],',bCrlf);
sb.AppendLine(' "cnpj": "00003555500153",',bCrlf);
sb.AppendLine(' "rua": "AV ABCDEF PINTO CATAO, 18",',bCrlf);
sb.AppendLine(' "complemento": "",',bCrlf);
sb.AppendLine(' "contatos": [',bCrlf);
sb.AppendLine(' {',bCrlf);
sb.AppendLine(' "telefones": [',bCrlf);
sb.AppendLine(' {',bCrlf);
sb.AppendLine(' "numero": "1999655554",',bCrlf);
sb.AppendLine(' "tipo": "T",',bCrlf);
sb.AppendLine(' "id": 2555559',bCrlf);
sb.AppendLine(' }',bCrlf);
sb.AppendLine(' ],',bCrlf);
sb.AppendLine(' "cargo": "zzz de compras",',bCrlf);
sb.AppendLine(' "nome": "Gerard",',bCrlf);
sb.AppendLine(' "emails": [',bCrlf);
sb.AppendLine(' {',bCrlf);
sb.AppendLine(' "email": "gerard@terra.com.br",',bCrlf);
sb.AppendLine(' "tipo": "T",',bCrlf);
sb.AppendLine(' "id": 1065559',bCrlf);
sb.AppendLine(' }',bCrlf);
sb.AppendLine(' ],',bCrlf);
sb.AppendLine(' "id": 844485,',bCrlf);
sb.AppendLine(' "excluido": false',bCrlf);
sb.AppendLine(' }',bCrlf);
sb.AppendLine(' ],',bCrlf);
sb.AppendLine(' "tipo": "J",',bCrlf);
sb.AppendLine(' "razao_social": "TIDY TECNOLOGIA LTDA - EPP",',bCrlf);
sb.AppendLine(' "nome_fantasia": "TIDY",',bCrlf);
sb.AppendLine(' "bairro": "TUNA",',bCrlf);
sb.AppendLine(' "cidade": "JAGUAR",',bCrlf);
sb.AppendLine(' "inscricao_estadual": "395.222.441.222",',bCrlf);
sb.AppendLine(' "observacao": "ligar sempre depois das 14hs",',bCrlf);
sb.AppendLine(' "id": 2255597,',bCrlf);
sb.AppendLine(' "ultima_alteracao": "2016-12-28 07:31:52",',bCrlf);
sb.AppendLine(' "cep": "13555500",',bCrlf);
sb.AppendLine(' "suframa": "",',bCrlf);
sb.AppendLine(' "estado": "SP",',bCrlf);
sb.AppendLine(' "emails": [',bCrlf);
sb.AppendLine(' {',bCrlf);
sb.AppendLine(' "email": "xi@tidy.com.br",',bCrlf);
sb.AppendLine(' "tipo": "T",',bCrlf);
sb.AppendLine(' "id": 10655558',bCrlf);
sb.AppendLine(' }',bCrlf);
sb.AppendLine(' ],',bCrlf);
sb.AppendLine(' "excluido": false',bCrlf);
sb.AppendLine(' }',bCrlf);
sb.AppendLine(']',bCrlf);
// Load the JSON array into a JsonArray:
jsonArray := TJsonArray.Create;
success := jsonArray.LoadSb(sb);
if (success <> True) then
begin
WriteLn(jsonArray.LastErrorText);
Exit;
end;
// Get some information from each record in the array.
numRecords := jsonArray.Size;
i := 0;
while i < numRecords do
begin
WriteLn('------ Record ' + i + ' -------');
jsonRecord := jsonArray.ObjectAt(i);
// Examine information for this record
numTelefones := jsonRecord.SizeOfArray('telefones');
WriteLn('Number of telefones: ' + numTelefones);
j := 0;
while j < numTelefones do
begin
jsonRecord.J := j;
WriteLn(' telefones numero: ' + jsonRecord.StringOf('telefones[j].numero'));
WriteLn(' telefones tipo: ' + jsonRecord.StringOf('telefones[j].tipo'));
WriteLn(' telefones id: ' + jsonRecord.StringOf('telefones[j].id'));
j := j + 1;
end;
WriteLn('cnpj: ' + jsonRecord.StringOf('cnpj'));
WriteLn('rua: ' + jsonRecord.StringOf('rua'));
// ...
numContatos := jsonRecord.SizeOfArray('contatos');
WriteLn('Number of contatos: ' + numContatos);
j := 0;
while j < numContatos do
begin
jsonRecord.J := j;
numTelefones := jsonRecord.SizeOfArray('contatos[j].telefones');
WriteLn(' Number of telefones: ' + numTelefones);
k := 0;
while k < numTelefones do
begin
jsonRecord.K := k;
WriteLn(' telefones numero: ' + jsonRecord.StringOf('contatos[j].telefones[k].numero'));
WriteLn(' telefones tipo: ' + jsonRecord.StringOf('contatos[j].telefones[k].tipo'));
WriteLn(' telefones id: ' + jsonRecord.StringOf('contatos[j].telefones[k].id'));
k := k + 1;
end;
WriteLn(' cargo: ' + jsonRecord.StringOf('contatos[j].cargo'));
numEmails := jsonRecord.SizeOfArray('contatos[j].emails');
WriteLn(' Number of emails: ' + numEmails);
k := 0;
while k < numEmails do
begin
jsonRecord.K := k;
WriteLn(' emails email: ' + jsonRecord.StringOf('contatos[j].emails[k].email'));
WriteLn(' emails tipo: ' + jsonRecord.StringOf('contatos[j].emails[k].tipo'));
WriteLn(' emails id: ' + jsonRecord.StringOf('contatos[j].emails[k].id'));
k := k + 1;
end;
j := j + 1;
end;
jsonRecord.Free;
i := i + 1;
end;
// The output for the above code is:
// ------ Record 0 -------
// Number of telefones: 1
// telefones numero: 19995555555
// telefones tipo: T
// telefones id: 2541437
// cnpj: 11395551000164
// rua: R XAVIER AUGUSTO ROGGE, 22
// Number of contatos: 0
// ------ Record 1 -------
// Number of telefones: 0
// cnpj: 12496555500180
// rua: AV ROLF WIEST, 100
// Number of contatos: 0
// ------ Record 2 -------
// Number of telefones: 1
// telefones numero: 1938655556
// telefones tipo: T
// telefones id: 2555438
// cnpj: 00003555500153
// rua: AV ABCDEF PINTO CATAO, 18
// Number of contatos: 1
// Number of telefones: 1
// telefones numero: 1999655554
// telefones tipo: T
// telefones id: 2555559
// cargo: zzz de compras
// Number of emails: 1
// emails email: gerard@terra.com.br
// emails tipo: T
// emails id: 1065559
//
sb.Free;
jsonArray.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.