Einzelnen Beitrag anzeigen

Benutzerbild von Olli73
Olli73

Registriert seit: 25. Apr 2008
Ort: Neunkirchen
669 Beiträge
 
#18

AW: TJSON.JSONToObject DateTime als LocalTime

  Alt 1. Nov 2023, 10:48
Also bei mir funktioniert das hier auch (Delphi 11):

Delphi-Quellcode:
unit Unit7;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, REST.Json;

type
  TMyClass = Class
    Test: TDateTime;
  End;

  TForm7 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form7: TForm7;

implementation

{$R *.dfm}

procedure TForm7.btn1Click(Sender: TObject);
var
  dt: TDateTime;
  MyObj: TMyClass;
  S: String;
begin
  MyObj := TMyClass.Create;
  try
    MyObj.Test := Now;
    S := TJSON.ObjectToJsonString(MyObj, [joDateFormatISO8601]);
    ShowMessage(S);

    MyObj.Free;
    MyObj := TJson.JsonToObject<TMyClass>(S, [joDateFormatISO8601]);
    ShowMessage(DateTimeToStr(MyObj.Test));

  finally
    MyObj.Free;
  end;

end;

end.
  Mit Zitat antworten Zitat