Registriert seit: 19. Jun 2006
647 Beiträge
Delphi 11 Alexandria
|
AW: JSON Add auch mit Objekts?
16. Jul 2024, 17:51
Du kannst bei BeginObject einen Namen (KEY) mitgeben:
Delphi-Quellcode:
uses
System.DateUtils,
System.JSON.Types,
System.JSON.Writers,
System.JSON.Builders;
procedure TForm1.FormCreate(Sender: TObject);
var
Builder: TJSONObjectBuilder;
Writer: TJsonTextWriter;
StringWriter: TStringWriter;
StringBuilder: TStringBuilder;
begin
StringBuilder := TStringBuilder.Create;
StringWriter := TStringWriter.Create(StringBuilder);
Writer := TJsonTextWriter.Create(StringWriter);
Writer.Formatting := TJsonFormatting.Indented;
Builder := TJSONObjectBuilder.Create(Writer);
Builder
.BeginObject
.Add('NAME','Wert')
.BeginObject('METADATA')
.Add('start_datetime', Now)
.Add('end_datetime', IncHour(Now))
.Add('origin_file', 'FileName.Par')
.Add('pm_version', 'V8.5.5')
.EndObject
.EndObject;
Memo1.Lines.Text := StringBuilder.ToString;
end;
Das erzeugt foglenden JSON:
Code:
{
"NAME": "Wert",
"METADATA": {
"start_datetime": "2024-07-16T17:53:21.157+02:00",
"end_datetime": "2024-07-16T18:53:21.157+02:00",
"origin_file": "FileName.Par",
"pm_version": "V8.5.5"
}
}
Geändert von Rolf Frei (16. Jul 2024 um 18:08 Uhr)
|
|
Zitat
|