AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

JSONObj := TJson.ObjectToJsonObject(myObj)

Ein Thema von Edelfix · begonnen am 23. Apr 2018 · letzter Beitrag vom 23. Apr 2018
 
Edelfix

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

JSONObj := TJson.ObjectToJsonObject(myObj)

  Alt 23. Apr 2018, 10: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
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:53 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz