Einzelnen Beitrag anzeigen

Schokohase
(Gast)

n/a Beiträge
 
#24

AW: Gehen strings als Eigenschaftsnamen? Wenn ja wie?

  Alt 7. Okt 2018, 23:24
Gibt es eigentlich einen Grund dafür warum du nicht einfach eine Klasse mit den passenden Eigenschaften deklarierst und dann mit Delphi-Referenz durchsuchenREST.Json.TJson.JsonToObject arbeitest?

Die GroupOptions würden z.B. in diese Klasse passen
Delphi-Quellcode:
uses
  REST.Json.Types;

TGroupOptionsData = class
private
  [JSONName( 'ChatID' )]
  FChatId: string;
  [JSONName( 'DeleteSpam' )]
  FDeleteSpam: Boolean;
  [JSONName( 'RestrictNewUser' )]
  FRestrictNewUser: Boolean;
  [JSONName( 'AntiSpamMessage' )]
  FAntiSpamMessage: string;
  [JSONName( 'can_send_messages' )]
  FCanSendMessages: Boolean;
  [JSONName( 'can_send_media_messages' )]
  FCanSendMediaMessages: Boolean;
  [JSONName( 'can_send_other_messages' )]
  FCanSendOtherMessages: Boolean;
  [JSONName( 'can_add_web_page_previews' )]
  FCanAddWebPagePreviews: Boolean;
  [JSONName( 'Creator' )]
  FCreator: string;
  [JSONName( 'blacklist' )]
  FBlacklist: string;
  [JSONName( 'whitelist' )]
  FWhitelist: string;
  [JSONName( 'restrict_time' )]
  FRestrictTime: Integer;
  [JSONName( 'welcomemessage' )]
  FWelcomeMessage: string;
  [JSONName( 'start' )]
  FStart: Boolean;
public
  property ChatId: string read FChatId write FChatId;
  property DeleteSpam: Boolean read FDeleteSpam write FDeleteSpam;
  property RestrictNewUser: Boolean read FRestrictNewUser write FRestrictNewUser;
  property AntiSpamMessage: string read FAntiSpamMessage write FAntiSpamMessage;
  property CanSendMessages: Boolean read FCanSendMessages write FCanSendMessages;
  property CanSendMediaMessages: Boolean read FCanSendMediaMessages write FCanSendMediaMessages;
  property CanSendOtherMessages: Boolean read FCanSendOtherMessages write FCanSendOtherMessages;
  property CanAddWebPagePreviews: Boolean read FCanAddWebPagePreviews write FCanAddWebPagePreviews;
  property Creator: string read FCreator write FCreator;
  property Blacklist: string read FBlacklist write FBlacklist;
  property Whitelist: string read FWhitelist write FWhitelist;
  property RestrictTime: Integer read FRestrictTime write FRestrictTime;
  property WelcomeMessage: string read FWelcomeMessage write FWelcomeMessage;
  property Start: Boolean read FStart write FStart;
end;
und dann einfach
Delphi-Quellcode:
uses
  REST.Json;

const
  jsonstr = //
    ' {' + sLineBreak + //
    ' "ChatID": "-xxxxxx",' + sLineBreak + //
    ' "DeleteSpam": true,' + sLineBreak + //
    ' "RestrictNewUser": true,' + sLineBreak + //
    ' "AntiSpamMessage": "SpamPost",' + sLineBreak + //
    ' "can_send_messages": true,' + sLineBreak + //
    ' "can_send_media_messages": false,' + sLineBreak + //
    ' "can_send_other_messages": false,' + sLineBreak + //
    ' "can_add_web_page_previews": false,' + sLineBreak + //
    ' "Creator": "blablub",' + sLineBreak + //
    ' "blacklist": "blablub",' + sLineBreak + //
    ' "whitelist": "",' + sLineBreak + //
    ' "restrict_time": 15,' + sLineBreak + //
    ' "welcomemessage": "",' + sLineBreak + //
    ' "start": true' + sLineBreak + //
    ' }';
var
  options: TGroupOptionsData;
begin
  options := TJson.JsonToObject<TGroupOptionsData>(jsonstr);
  try
    // ...
  finally
    options.Free;
  end;
end;
  Mit Zitat antworten Zitat