Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.070 Beiträge
 
Delphi 10.4 Sydney
 
#2

AW: JSONObj := TJson.ObjectToJsonObject(myObj)

  Alt 23. Apr 2018, 11:12
Na ja, es fehlt halt an FOccupancies. Da kann dann natürlich kein Array gebildet werden.
Sowas würde ich aber nicht mit der Hand machen, nutze den praktischen Json To Delphi Class Generator von Petar Georgiev.

Delphi-Quellcode:
unit RootUnit;

// *************************************************
// Generated By: JsonToDelphiClass - 0.65
// Project link: https://github.com/PKGeorgiev/Delphi-JsonToDelphiClass
// Generated On: 2018-04-23 11:08:41
// *************************************************
// Created By : Petar Georgiev - 2014
// WebSite : http://pgeorgiev.com
// *************************************************

interface

uses Generics.Collections, Rest.Json;

type

TOccupanciesClass = class
private
  FAdults: String;
  FChildren: String;
  FRooms: String;
public
  property adults: String read FAdults write FAdults;
  property children: String read FChildren write FChildren;
  property rooms: String read FRooms write FRooms;
  function ToJsonString: string;
  class function FromJsonString(AJsonString: string): TOccupanciesClass;
end;

TDestinationClass = class
private
  FCode: String;
public
  property code: String read FCode write FCode;
  function ToJsonString: string;
  class function FromJsonString(AJsonString: string): TDestinationClass;
end;

TStayClass = class
private
  FCheckIn: TDate;
  FCheckOut: TDate;
public
  property checkIn: TDate read FCheckIn write FCheckIn;
  property checkOut: TDate read FCheckOut write FCheckOut;
  function ToJsonString: string;
  class function FromJsonString(AJsonString: string): TStayClass;
end;

TRootClass = class
private
  FDestination: TDestinationClass;
  FOccupancies: TArray<TOccupanciesClass>;
  FStay: TStayClass;
public
  property destination: TDestinationClass read FDestination write FDestination;
  property occupancies: TArray<TOccupanciesClass> read FOccupancies write FOccupancies;
  property stay: TStayClass read FStay write FStay;
  constructor Create;
  destructor Destroy; override;
  function ToJsonString: string;
  class function FromJsonString(AJsonString: string): TRootClass;
end;

implementation

{TOccupanciesClass}


function TOccupanciesClass.ToJsonString: string;
begin
  result := TJson.ObjectToJsonString(self);
end;

class function TOccupanciesClass.FromJsonString(AJsonString: string): TOccupanciesClass;
begin
  result := TJson.JsonToObject<TOccupanciesClass>(AJsonString)
end;

{TDestinationClass}


function TDestinationClass.ToJsonString: string;
begin
  result := TJson.ObjectToJsonString(self);
end;

class function TDestinationClass.FromJsonString(AJsonString: string): TDestinationClass;
begin
  result := TJson.JsonToObject<TDestinationClass>(AJsonString)
end;

{TStayClass}


function TStayClass.ToJsonString: string;
begin
  result := TJson.ObjectToJsonString(self);
end;

class function TStayClass.FromJsonString(AJsonString: string): TStayClass;
begin
  result := TJson.JsonToObject<TStayClass>(AJsonString)
end;

{TRootClass}

constructor TRootClass.Create;
begin
  inherited;
  FStay := TStayClass.Create();
  FDestination := TDestinationClass.Create();
end;

destructor TRootClass.Destroy;
var
  LoccupanciesItem: TOccupanciesClass;
begin

 for LoccupanciesItem in FOccupancies do
   LoccupanciesItem.free;

  FStay.free;
  FDestination.free;
  inherited;
end;

function TRootClass.ToJsonString: string;
begin
  result := TJson.ObjectToJsonString(self);
end;

class function TRootClass.FromJsonString(AJsonString: string): TRootClass;
begin
  result := TJson.JsonToObject<TRootClass>(AJsonString)
end;

end.
  Mit Zitat antworten Zitat