Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
Load JSON Object from HTTP Response Body
Demonstrates how to load the HTTP response body directly into a JSON object.Chilkat Lazarus Pascal Downloads
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.Http,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
resp: THttpResponse;
json: TJsonObject;
begin
success := False;
http := THttp.Create;
resp := THttpResponse.Create;
success := http.HttpStr('GET','https://www.chilkatsoft.com/exampledata/sample.json','','','',resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
// A JSON object is JSON that begins with "{" and ends with "}"
json := TJsonObject.Create;
// If we wish to transfer (instead of copy) the JSON from the HttpResponse to the JsonObject, then add the keyword "TakeResponseBody" to UncommonOptions
// This could save memory for extremely large JSON responses.
resp.UncommonOptions := 'TakeResponseBody';
resp.GetBodyJson(json);
json.EmitCompact := False;
WriteLn(json.Emit());
// Note: If UncommonOptions contained "TakeResponseBody", then the response BodyStr will now be empty:
WriteLn('----');
WriteLn(resp.BodyStr);
http.Free;
resp.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.