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