Hallo,
irgendwie komme ich hier nicht weiter.
Ich möchte ein Json File in mein Object wandeln und zurück.
Das Json File:
Code:
{
"stay": {
"checkIn": "2019-03-15",
"checkOut": "2019-03-16"
},
"destination": {
"code": "PMI"
},
"occupancies": [{
"rooms": "1",
"adults": "2",
"children": "0"
}]
}
mein Object:
Delphi-Quellcode:
uses REST.Json, System.Json;
interface
Tstay = class(TObject)
private
FcheckIn: String;
FcheckOut: String;
public
property checkIn : String read FcheckIn write FcheckIn;
property checkOut : String read FcheckOut write FcheckOut;
end;
Tdestination = class
private
Fcode: String;
public
property code : String read Fcode write Fcode;
end;
TByDest = class(TObject)
private
Fstay: Tstay;
Fdestination: Tdestination;
function Getoccupancies(const Name: string): string;
procedure Setoccupancies(const Name, Value: string);
public
constructor Create;
destructor Destroy;
property stay : Tstay read Fstay write Fstay;
property destination : Tdestination read Fdestination write Fdestination;
property occupancies[const Name: string] : string read Getoccupancies write Setoccupancies;
end;
var
Data : TStringList;
implementation
constructor TByDest.Create;
begin
inherited;
self.stay := Tstay.Create;
self.destination := Tdestination.Create;
Data := TStringList.Create;
end;
destructor TByDest.Destroy;
begin
stay.Free;
destination.Free;
Data.Free;
inherited;
end;
function TByDest.Getoccupancies(const Name: string): string;
begin
Result := Data.Values[Name];
end;
procedure TByDest.Setoccupancies(const Name, Value: string);
begin
Data.Add(Name+'='+Value);
end;
Mein Test Aufruf:
procedure TMainForm.Button2Click(Sender: TObject);
var
myObj: TByDest;
JSONObj: TJsonObject;
begin
myObj:= TByDest.Create;
JSONObj := TJson.ObjectToJsonObject(myObj);
Memo1.Lines.Add(TJson.Format(JSONObj));
end;
das Array wird aber nicht in Json umgesetzt. Ich habe den Verdacht dass das Array in meinem Object falsch ist. Hat jemand eine Idee?