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

Example: JsonObject.NewObjectOf method

Demonstrates the NewObjectOf method.

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
  success: Boolean;
  car: string;
  json: TJsonObject;

begin
  success := False;

  car := '{"make":"Toyota","model":"Camry","year":2022,"color":"silver","engine":{"type":"inline-4","fuel":"gasoline","horsepower":203},"features":["bluetooth","backup camera","adaptive cruise control","lane assist"],"isElectric":false}';

  json := TJsonObject.Create;

  json.UpdateString('test','abc');

  success := json.NewObjectOf('car',car);
  if (success = False) then
    begin
      WriteLn(json.LastErrorText);
      Exit;
    end;

  json.EmitCompact := False;
  WriteLn(json.Emit());

  //  Result:

  //  {
  //    "test": "abc",
  //    "car": {
  //      "make": "Toyota",
  //      "model": "Camry",
  //      "year": 2022,
  //      "color": "silver",
  //      "engine": {
  //        "type": "inline-4",
  //        "fuel": "gasoline",
  //        "horsepower": 203
  //      },
  //      "features": [
  //        "bluetooth",
  //        "backup camera",
  //        "adaptive cruise control",
  //        "lane assist"
  //      ],
  //      "isElectric": false
  //    }
  //  }


  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.