如何在Delphi中打印JSON?

时间:2021-09-25 21:44: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).

我正在寻找一个函数,它将一串JSON作为输入,并使用换行符和缩进(制表符)对其进行格式化。

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?

我发现了许多PHP和C#的例子,但不是Delphi。有人可以帮忙这样的功能吗?

Update - Solution with SuperObject:

更新 - 使用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;

4 个解决方案

#1


5  

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

使用superobject库,确保使用存储库文件中的最新版本,而不是1.2.4 ZIP。

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

然后你可以用.AsJSON(true)格式化你的TSuperObject对象('true'可以解决这个问题)。

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

[请注意,您无法控制JSON字段的显示顺序]

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

[并从字符串创建对象:var lJSON:ISuperObject; lJSON:= SO(字符串); ]

#2


8  

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.

如果您不想使用任何外部库,并且您正在使用delphi XE5或更新版本,则REST.Json单元中有一个非常方便的TJson.Format()函数。

uses json, REST.Json;

{ ... }    

function FormatJSON(json: String): String;
var
  tmpJson: TJsonObject;
begin
  tmpJson := TJSONObject.ParseJSONValue(json);
  Result := TJson.Format(tmpJson);

  FreeAndNil(tmpJson);
end;

#3


3  

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

您也可以使用我们的开源SynCommons.pas单元的以下方法:

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:

如果使用jsonUnquotedPropName格式:

  new := JSONReformat(json,jsonUnquotedPropName);

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

您将获得以下扩展语法(类似于JavaScript或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.

此语法被接受为我们的开源框架的所有JSON函数的有效输入,作为默认JSON语法的替代。我们发现它很有用,例如用于配置文件。

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.

请注意,我们的JSONReformat()函数非常快。它在1.4秒内将CityLots中巨大的190 MB不可格式JSON内容转换为400 MB的美化JSON(预定和带有行字段)。 SuperObject只能在10秒内读取它,并且仅使用1.1 GB来存储190 MB的内容。 DBXJSON甚至无法加载数据:它消耗所有32位内存 - 在Win64(XE6)下,需要50秒并使用3 GB的RAM来读取190 MB的JSON。有些数字,请参阅此文章。

#4


1  

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

如果您正在使用Delphi XE或更新版本,则可以使用delphi-xe-json库

function PrettyPrint (aJSON : string) : string;
var
  jo : IJSONObject
begin
  jo := TJSON.NewObject(aJSON);
  result := jo.ToString(true);
end;

#1


5  

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

使用superobject库,确保使用存储库文件中的最新版本,而不是1.2.4 ZIP。

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

然后你可以用.AsJSON(true)格式化你的TSuperObject对象('true'可以解决这个问题)。

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

[请注意,您无法控制JSON字段的显示顺序]

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

[并从字符串创建对象:var lJSON:ISuperObject; lJSON:= SO(字符串); ]

#2


8  

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.

如果您不想使用任何外部库,并且您正在使用delphi XE5或更新版本,则REST.Json单元中有一个非常方便的TJson.Format()函数。

uses json, REST.Json;

{ ... }    

function FormatJSON(json: String): String;
var
  tmpJson: TJsonObject;
begin
  tmpJson := TJSONObject.ParseJSONValue(json);
  Result := TJson.Format(tmpJson);

  FreeAndNil(tmpJson);
end;

#3


3  

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

您也可以使用我们的开源SynCommons.pas单元的以下方法:

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:

如果使用jsonUnquotedPropName格式:

  new := JSONReformat(json,jsonUnquotedPropName);

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

您将获得以下扩展语法(类似于JavaScript或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.

此语法被接受为我们的开源框架的所有JSON函数的有效输入,作为默认JSON语法的替代。我们发现它很有用,例如用于配置文件。

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.

请注意,我们的JSONReformat()函数非常快。它在1.4秒内将CityLots中巨大的190 MB不可格式JSON内容转换为400 MB的美化JSON(预定和带有行字段)。 SuperObject只能在10秒内读取它,并且仅使用1.1 GB来存储190 MB的内容。 DBXJSON甚至无法加载数据:它消耗所有32位内存 - 在Win64(XE6)下,需要50秒并使用3 GB的RAM来读取190 MB的JSON。有些数字,请参阅此文章。

#4


1  

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

如果您正在使用Delphi XE或更新版本,则可以使用delphi-xe-json库

function PrettyPrint (aJSON : string) : string;
var
  jo : IJSONObject
begin
  jo := TJSON.NewObject(aJSON);
  result := jo.ToString(true);
end;