[fpc-pascal] Read JSON from file
Michael Van Canneyt
michael at freepascal.org
Fri Aug 7 08:33:43 CEST 2015
On Fri, 7 Aug 2015, Felipe Monteiro de Carvalho wrote:
> 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.
As it happens, I have yesterday committed an extension which allows you to specify that the JSON supports comments.
Both // and /* */ are detected and discarded.
Michael.
More information about the fpc-pascal
mailing list