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

JSON AppendObject2 Example

Demonstrates the AppendObject2 function.

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

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

procedure RunDemo;
var
  json: TJsonObject;
  jObj: TJsonObject;

begin
  json := TJsonObject.Create;
  json.Load('{ "name": "John", "marbles": 100 }');

  //  Append an empty object named "addr"
  jObj := TJsonObject.Create;
  json.AppendObject2('addr',jObj);

  WriteLn(json.Emit());
  //  Expected output is:   {"name":"John","marbles":100,"addr":{}}

  //  Add members to the object.
  jObj.AppendString('street','1200 Elm St.');
  jObj.AppendString('city','Springfield');
  jObj.AppendInt('zip',60606);

  WriteLn(json.Emit());
  //  Expected output is:  {"name":"John","marbles":100,"addr":{"street":"1200 Elm St.","city":"Springfield","zip":60606}}


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