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

JSON Array AddObjectCopyAt Example

Demonstrates the AddObjectCopyAt function.

Note: This example requires Chilkat v9.5.0.82 or above.

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.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  jarr: TJsonArray;
  json: TJsonObject;

begin
  jarr := TJsonArray.Create;
  jarr.Load('[ 1, 2, 3, 4]');

  json := TJsonObject.Create;
  json.Load('{"street":"1200 Elm St.","city":"Springfield","zip":60606}');

  //  Copy the contents of json to the array at index 2, making it the 3rd item in the array.
  jarr.AddObjectCopyAt(2,json);

  WriteLn(jarr.Emit());
  //  Expected output is:  [1,2,{"street":"1200 Elm St.","city":"Springfield","zip":60606},3,4]


  jarr.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.