Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Load a JSON Array

See more JSON Examples

The Chilkat JSON API requires the top-level JSON to be an object. Therefore, to load an array requires that it first be wrapped as an object.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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;
  jsonArrayStr: string;
  sbJson: TStringBuilder;
  json: TJsonObject;
  jArray: TJsonArray;
  jObjId: TJsonObject;

begin
  success := False;

  //  Imagine we want to load this JSON array for parsing:
  jsonArrayStr := '[{"id":200},{"id":196}]';

  //  First wrap it in a JSON object by prepending "{ "array":" and appending "}"
  sbJson := TStringBuilder.Create;
  sbJson.Append('{"array":');
  sbJson.Append(jsonArrayStr);
  sbJson.Append('}');

  json := TJsonObject.Create;
  json.Load(sbJson.GetAsString());

  //  Now we can get the JSON array
  jArray := json.ArrayAt(0);

  //  Do what you want with the JSON array...
  //  For example:
  jObjId := jArray.ObjectAt(0);
  WriteLn(jObjId.IntOf('id'));
  jObjId.Free;

  jArray.Free;


  sbJson.Free;
  json.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.