Lazarus

Programming => General => Topic started by: Marion on May 08, 2021, 12:30:36 am

Title: HOW TO: Write TJSONArray or TJSONObject to a text file
Post by: Marion on May 08, 2021, 12:30:36 am
I am studying the fpjson unit but I can't see how to read and write the json to disk?
Title: Re: HOW TO: Write TJSONArray or TJSONObject to a text file
Post by: lainz on May 08, 2021, 03:45:06 am
Using a tstringlist. Create a tstringlist assign the json text and save it to file.

I recommend you jsontools, is faster.
Title: Re: HOW TO: Write TJSONArray or TJSONObject to a text file
Post by: Gustavo 'Gus' Carreno on May 08, 2021, 07:42:10 am
Hey Marion,

I am studying the fpjson unit but I can't see how to read and write the json to disk?

I have these 2 function that I use to get my TJSONData objects:
Code: Pascal  [Select][+][-]
  1. function GetJSONData(const aJSON: UTF8String): TJSONData;
  2. var
  3.   jParser: TJSONParser;
  4. begin
  5.   Result := nil;
  6. {$IF FPC_FULLVERSION >= 30002}
  7.   jParser := TJSONParser.Create(aJSON, [joUTF8, joIgnoreTrailingComma]);
  8. {$ELSE}
  9.   jParser := TJSONParser.Create(aJSON, True);
  10. {$ENDIF}
  11.   try
  12.     Result := jParser.Parse;
  13.   finally
  14.     jParser.Free;
  15.   end;
  16. end;
  17.  
  18. function GetJSONData(const aStream: TStream): TJSONData;
  19. var
  20.   jParser: TJSONParser;
  21. begin
  22.   Result:= nil;
  23.   aStream.Position:= 0;
  24. {$IF FPC_FULLVERSION >= 30002}
  25.   jParser:= TJSONParser.Create(aStream, [joUTF8, joIgnoreTrailingComma]);
  26. {$ELSE}
  27.   jParser := TJSONParser.Create(aStream, True);
  28. {$ENDIF}
  29.   try
  30.     Result:= jParser.Parse;
  31.   finally
  32.     jParser.Free;
  33.   end;
  34. end;

This makes it easy to then do this:
Code: Pascal  [Select][+][-]
  1. var
  2.   stringStream: TStringStream;
  3.   jsonRoot: TJSONData;
  4. begin
  5.   // Using GetJSONData with a String
  6.   stringStream:= TStringStream.Create('', TEncoding.UTF8);
  7.   stringStream.LoadFromFile('/path/to/the/JSON/file');
  8.   jsonRoot:= GetJSONData(stringStream.UnicodeDataString);
  9.   stringStream.Free
  10. end;
or this:
Code: Pascal  [Select][+][-]
  1. var
  2.   fileStream: TFileStream;
  3.   jsonRoot: TJSONData;
  4. begin
  5.   // Using GetJSONData with a Stream
  6.   fileStream:= TFileStream.Create('/path/to/the/JSON/file', fmOpenRead);
  7.   jsonRoot:= GetJSONData(fileStream);
  8.   fileStream.Free
  9. end;

If you then want to save:
Code: Pascal  [Select][+][-]
  1. var
  2.   stringStream: TStringStream;
  3.   jsonRoot: TJSONData;
  4. begin
  5.   stringStream:= TStringStream.Create(jsonRoot.AsJson, TEncoding.UTF8);
  6.   stringStream.SaveToFile('/path/to/the/JSON/file');
  7.   stringStream.Free
  8. end;

Hope this helps get you started.

Cheers,
Gus
Title: Re: HOW TO: Write TJSONArray or TJSONObject to a text file
Post by: Marion on May 09, 2021, 09:36:54 pm
Thanks
Title: Re: HOW TO: Write TJSONArray or TJSONObject to a text file
Post by: Gustavo 'Gus' Carreno on May 10, 2021, 12:07:56 am
Hey Marion,

Thanks

You're more than welcome.

Cheers,
Gus
TinyPortal © 2005-2018