Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have adopted the code from Mason, did the reader exercise, and put it in a separate unit:</p> <pre><code>unit uJSONTools; interface Uses Classes, SysUtils, DBXJSON; procedure PrettyPrintJSON(JSONValue: TJSONValue; OutputStrings: TStrings; indent: integer = 0); // Formats JSONValue to an indented structure and adds it to OutputStrings implementation const INDENT_SIZE = 2; procedure PrettyPrintPair(JSONValue: TJSONPair; OutputStrings: TStrings; last: boolean; indent: integer); const TEMPLATE = '%s : %s'; var line: string; newList: TStringList; begin newList := TStringList.Create; try PrettyPrintJSON(JSONValue.JsonValue, newList, indent); line := format(TEMPLATE, [JSONValue.JsonString.ToString, Trim(newList.Text)]); finally newList.Free; end; line := StringOfChar(' ', indent * INDENT_SIZE) + line; if not last then line := line + ','; OutputStrings.add(line); end; procedure PrettyPrintArray(JSONValue: TJSONArray; OutputStrings: TStrings; last: boolean; indent: integer); var i: integer; begin OutputStrings.add(StringOfChar(' ', indent * INDENT_SIZE) + '['); for i := 0 to JSONValue.size - 1 do PrettyPrintJSON(JSONValue.Get(i), OutputStrings, indent + 1); OutputStrings.add(StringOfChar(' ', indent * INDENT_SIZE) + ']'); end; procedure PrettyPrintJSON(JSONValue: TJSONValue; OutputStrings: TStrings; indent: integer = 0); var i: integer; begin if JSONValue is TJSONObject then begin OutputStrings.add(StringOfChar(' ', indent * INDENT_SIZE) + '{'); for i := 0 to TJSONObject(JSONValue).size - 1 do PrettyPrintPair(TJSONObject(JSONValue).Get(i), OutputStrings, i = TJSONObject(JSONValue).size - 1, indent + 1); OutputStrings.add(StringOfChar(' ', indent * INDENT_SIZE) + '}'); end else if JSONValue is TJSONArray then PrettyPrintArray(TJSONArray(JSONValue), OutputStrings, i = TJSONObject(JSONValue).size - 1, indent + 1) else OutputStrings.add(StringOfChar(' ', indent * INDENT_SIZE) + JSONValue.ToString); end; end. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload