Einzelnen Beitrag anzeigen

Edelfix

Registriert seit: 6. Feb 2015
Ort: Stadtoldendorf
213 Beiträge
 
Delphi 10.4 Sydney
 
#1

JSONObj := TJson.ObjectToJsonObject(myObj)

  Alt 23. Apr 2018, 09:48
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?
  Mit Zitat antworten Zitat