I am looking for a function that will take a string of JSON as input and format it with line breaks and indentations (tabs).
Example: I have input line:
{"menu": {"header": "JSON viewer", "items": [{"id": "Delphi"},{"id": "Pascal", "label": "Nice tree format"}, null]}}
And want to get a readable result as text:
{
"menu":{
"header":"JSON viewer",
"items":[
{
"id":"Delphi"
},
{
"id":"Pascal",
"label":"Nice tree format"
},
null
]
}
}
I found a lot of examples for PHP and C#, but not for Delphi. Could someone help with such a function?
Update - Solution with SuperObject:
function FormatJson (InString: WideString): string; // Input string is "InString"
var
Json : ISuperObject;
begin
Json := TSuperObject.ParseString(PWideChar(InString), True);
Result := Json.AsJson(true, false); //Here comes your result: pretty-print JSON
end;
WideString
, you wouldn't need any of that conversion code; the compiler would the equivalent task automatically any time you calledFormatJson
. Just change the type, and you can replace the first seven lines withJson := TSuperObject.ParseString(PWideChar(InString), True)
. – Carlyn