[fpc-pascal] Read JSON from file

Michael Van Canneyt michael at freepascal.org
Fri Aug 7 08:32:33 CEST 2015



On Thu, 6 Aug 2015, Chris Moody wrote:

> Hi all,
>
> For my current project, I download a file from a server that contains JSON 
> code. I'm not sure how to read it into something that GetJSON is able to 
> handle.
>
> My first thought was using TStrings, however not sure how to convert a 
> TString into TStream.
>
> Any assistance would be much appreciated.

The following will read JSON and write all keys it finds

uses jsonparser, fpjson, sysutils, classes;

Var
   F : TFileStream;
   D : TJSONData;
   E : TJSONEnum;

begin
   D:=Nil;
   F:=TFileStream.Create('myfile.json',fmOpenRead or fmShareDenywrite);
   try
     D:=GetJSON(F);
     for E in D do
       Writeln(E.Key, ' : ',E.Value.AsJSON);
   Finally
     D.free;
     F.Free;
   end;
end.

Michael.



More information about the fpc-pascal mailing list