[fpc-pascal] Formating JSON outputs

luciano de souza luchyanus at gmail.com
Tue Jan 28 12:29:33 CET 2014


Hello all,

JSON is a very easy format to handle with Freepascal. However, the
output is not so readable when the number of registers increase. An
example of it is:

program test;
{$mode objfpc}{$H+}

uses
classes, fpjson;

var
r, j: TJSONObject;
a: TJSONArray;
s: TStringlist;

BEGIN
s := TStringlist.create;
r := TJSONObject.create;
a := TJSONarray.create;
j := TJsonObject.create;
j.strings['name'] := 'Luciano de Souza';
j.integers['age'] := 38;
j.strings['city'] := 'NiterĂ³i';
j.strings['country'] := 'Brazil';
a.add(j.clone);
j.clear;
j.strings['name'] := 'Mariella Molinari';
j.integers['age'] := 29;
j.strings['city'] := 'Padova';
j.strings['country'] := 'Italy';
a.add(j.clone);
r.add('people', a);
s.text := r.AsJson;
s.SavetoFile('test.txt');
try

finally
j.free;
r.free;
s.free;
end;
END.

The corresponding output is:

{ "people" : [{ "name" : "Luciano de Souza", "age" : 38, "city" :
"NiterĂ³i", "country" : "Brazil" }, { "name" : "Mariella Molinari",
"age" : 29, "city" : "Padova", "country" : "Italy" }] }

My question is: is there a way to automatically format it? Breaking
lines? Becoming easy the reading?

-- 
Luciano de Souza



More information about the fpc-pascal mailing list