Forum > General

HOW TO: Write TJSONArray or TJSONObject to a text file

(1/1)

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

lainz:
Using a tstringlist. Create a tstringlist assign the json text and save it to file.

I recommend you jsontools, is faster.

Gustavo 'Gus' Carreno:
Hey Marion,


--- Quote from: 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?

--- End quote ---

I have these 2 function that I use to get my TJSONData objects:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function GetJSONData(const aJSON: UTF8String): TJSONData;var  jParser: TJSONParser;begin  Result := nil;{$IF FPC_FULLVERSION >= 30002}  jParser := TJSONParser.Create(aJSON, [joUTF8, joIgnoreTrailingComma]);{$ELSE}  jParser := TJSONParser.Create(aJSON, True);{$ENDIF}  try    Result := jParser.Parse;  finally    jParser.Free;  end;end; function GetJSONData(const aStream: TStream): TJSONData;var  jParser: TJSONParser;begin  Result:= nil;  aStream.Position:= 0;{$IF FPC_FULLVERSION >= 30002}  jParser:= TJSONParser.Create(aStream, [joUTF8, joIgnoreTrailingComma]);{$ELSE}  jParser := TJSONParser.Create(aStream, True);{$ENDIF}  try    Result:= jParser.Parse;  finally    jParser.Free;  end;end;
This makes it easy to then do this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  stringStream: TStringStream;  jsonRoot: TJSONData;begin  // Using GetJSONData with a String  stringStream:= TStringStream.Create('', TEncoding.UTF8);  stringStream.LoadFromFile('/path/to/the/JSON/file');  jsonRoot:= GetJSONData(stringStream.UnicodeDataString);  stringStream.Freeend;or this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  fileStream: TFileStream;  jsonRoot: TJSONData;begin  // Using GetJSONData with a Stream  fileStream:= TFileStream.Create('/path/to/the/JSON/file', fmOpenRead);  jsonRoot:= GetJSONData(fileStream);  fileStream.Freeend;
If you then want to save:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  stringStream: TStringStream;  jsonRoot: TJSONData;begin  stringStream:= TStringStream.Create(jsonRoot.AsJson, TEncoding.UTF8);  stringStream.SaveToFile('/path/to/the/JSON/file');  stringStream.Freeend;
Hope this helps get you started.

Cheers,
Gus

Marion:
Thanks

Gustavo 'Gus' Carreno:
Hey Marion,


--- Quote from: Marion on May 09, 2021, 09:36:54 pm ---Thanks

--- End quote ---

You're more than welcome.

Cheers,
Gus

Navigation

[0] Message Index

Go to full version