[fpc-pascal] Read JSON from file

Felipe Monteiro de Carvalho felipemonteiro.carvalho at gmail.com
Fri Aug 7 06:07:11 CEST 2015


On Thu, Aug 6, 2015 at 10:37 PM, Chris Moody
<inquiry at greensnakedesign.com> wrote:
> 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.

I do it like this, with TStringStream:

  lStrings := TStringList.Create;
  try
    lStrings.LoadFromFile(AFile);

    // Parse JSON data
    lStream := TStringStream.Create(lStrings.Text);
    lParser := TJSONParser.Create(lStream);
    try
      lParser.Strict := False;
      lData := lParser.Parse;
    finally
      lParser.Free;
      lStream.Free;
    end;

Well, in my particular case I use TStringList because I pre-process
the data, removing comments which are not allowed in JSON.

-- 
Felipe Monteiro de Carvalho



More information about the fpc-pascal mailing list