How do I pretty-print JSON in Delphi?

前端 未结 5 780
盖世英雄少女心
盖世英雄少女心 2020-12-31 19:22

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:



        
相关标签:
5条回答
  • 2020-12-31 19:50

    If you're working with Delphi XE or newer, you can use the delphi-xe-json library

    function PrettyPrint (aJSON : string) : string;
    var
      jo : IJSONObject
    begin
      jo := TJSON.NewObject(aJSON);
      result := jo.ToString(true);
    end;
    
    0 讨论(0)
  • 2020-12-31 19:51

    You may also use the following methods of our Open Source SynCommons.pas unit:

    var json,new: RawUTF8;
    begin
      json := '{"menu": {"header": "JSON viewer", "items": [{"id": "Delphi"},{"id": "Pascal", "label": "Nice tree format"}, null]}}';
      new := JSONReformat(json,jsonHumanReadable);
      ...
    

    Here new will contain:

    {
      "menu": {
        "header": "JSON viewer",
        "items": 
        [
          {
            "id": "Delphi"
          },
          {
            "id": "Pascal",
            "label": "Nice tree format"
          },
          null
        ]
      }
    }
    

    If you use the jsonUnquotedPropName format:

      new := JSONReformat(json,jsonUnquotedPropName);
    

    you will get the following extended syntax (similar to the one used in JavaScript or MongoDB shell):

    {
      menu: {
        header: "JSON viewer",
        items: 
        [
          {
            id: "Delphi"
          },
          {
            id: "Pascal",
            label: "Nice tree format"
          },
          null
        ]
      }
    }
    

    This syntax is accepted as valid input for all the JSON functions of our Open Source framework, as alternative to the default JSON syntax. We found it pretty useful, e.g. for configuration files.

    Note that our JSONReformat() function is very fast. It converts the huge 190 MB of unconformatable JSON content from CityLots into 400 MB of beautified JSON (intended and with line fields) in 1.4 seconds. SuperObject is just able to read it in 10 seconds, and uses 1.1 GB just for storing the 190 MB of content. And DBXJSON is not even able to load the data: it consumes all 32 bit memory - under Win64 (XE6), it takes 50 seconds and uses 3 GB of RAM to read the 190 MB of JSON. See this article for some numbers.

    0 讨论(0)
  • 2020-12-31 19:55

    If you do not want to use any external library, and you're using a delphi XE5 or newer, there is a very handy TJson.Format() function in the REST.Json unit.

    uses json, REST.Json;
    
    { ... }    
    
    function FormatJSON(json: String): String;
    var
      tmpJson: TJsonObject;
    begin
      tmpJson := TJSONObject.ParseJSONValue(json);
      Result := TJson.Format(tmpJson);
    
      FreeAndNil(tmpJson);
    end;
    
    0 讨论(0)
  • 2020-12-31 20:00

    This is a bit old, but if anyone is interested Delphi's native System.JSON unit can do this too. Sample uses a TMemo and a TButton to format the JSON

    procedure TForm1.btnFormatJSONClick(Sender: TObject);
    const
     DEF_INDENT = 2;
    var
     JO : TJSONObject;
    begin
     try
      JO := TJSONObject.ParseJSONValue(memoJSON.Text) as TJSONObject;
      memoJSON.Text := JO.Format(DEF_INDENT);
     except
      on E:Exception do
       begin
        MessageDlg('Error in JSON syntax', mtError, [mbOK], 0);
       end;
     end;
    end;
    
    0 讨论(0)
  • 2020-12-31 20:04

    Use the superobject library, make sure that you use the latest version from the repository file, not the 1.2.4 ZIP.

    Then you can format your TSuperObject object with .AsJSON(true) (the 'true' does the trick).

    [ Note that you have no control over the order in which the JSON fields are displayed ]

    [ And to create your object from the string: var lJSON : ISuperObject; lJSON := SO(string); ]

    0 讨论(0)
提交回复
热议问题