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

Using JSON StringOf with Non-String Members

See more JSON Examples

If a JSON member is a boolean, integer, or null, using StringOf will return its string representation.

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;
  a: string;
  b: string;
  c: string;
  ival: Integer;
  bval: Boolean;
  hasNull: Boolean;

begin
  //  Create JSON with members of different data types.
  json := TJsonObject.Create;

  json.UpdateInt('a',123);
  json.UpdateBool('b',True);
  json.UpdateNull('c');

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

  //  Resulting JSON:
  //  {
  //    "a": 123,
  //    "b": true,
  //    "c": null
  //  }

  a := json.StringOf('a');
  WriteLn(a);

  b := json.StringOf('b');
  WriteLn(b);

  c := json.StringOf('c');
  WriteLn(c);

  //  Output

  //  123
  //  true
  //  null

  //  If you want to get the integer, boolean, or null value
  //  you need to use the methods matching the data type
  ival := json.IntOf('a');
  bval := json.BoolOf('b');
  hasNull := json.IsNullOf('c');


  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.