Как отобразить JSON в читаемом виде?
// Способ первый
uses
{...,} System.JSON, REST.Json;
procedure TForm1.Button1Click(Sender: TObject);
var
json: string;
begin
json := '{"type1":199, "type2":"val2", "ar":["arval1","arval2"]}';
ShowMessage(TJSON.Format(TJSONObject.ParseJSONValue(json)));
end;
// Способ второй
uses
{...,} System.JSON;
procedure TForm1.Button1Click(Sender: TObject);
const
DEF_INDENT = 4;
var
json: string;
jo: TJSONObject;
begin
json := '{"type1":199, "type2":"val2", "ar":["arval1","arval2"]}';
jo := TJSONObject.ParseJSONValue(json) as TJSONObject;
ShowMessage(jo.Format(DEF_INDENT));
end;
// Или немного иначе
uses
{...,} System.JSON;
procedure TForm1.Button1Click(Sender: TObject);
const
DEF_INDENT = 4;
var
json: string;
jv: TJSONValue;
begin
json := '{"type1":199, "type2":"val2", "ar":["arval1","arval2"]}';
jv := TJSONObject.ParseJSONValue(json);
try
ShowMessage(jv.Format(DEF_INDENT));
finally
jv.Free;
end;
end;
|