Einzelnen Beitrag anzeigen

Schokohase
(Gast)

n/a Beiträge
 
#26

AW: Gehen strings als Eigenschaftsnamen? Wenn ja wie

  Alt 8. Okt 2018, 05:15
Schau dir mal https://jsontodelphi.com an.

Ein Nachteil ist allerdings, dass dort nicht mit dem JSONName Attribut gearbeitet wird (siehe mein Code).

Bei JSON sind die Eigenschaftsnamen case-sensitive bei Delphi nicht. Und dann passiert z.B. so etwas wie
Code:
{
  "foo": 1,
  "Foo": 2
}
Delphi-Quellcode:
unit RootUnit;

{*******************************************************************************
    Generated By  : JsonToDelphiClass - 0.65
    Project link  : https://github.com/PKGeorgiev/Delphi-JsonToDelphiClass
    Generated On  : 2018-10-08 01:16:30

    Created By    : Petar Georgiev - (http://pgeorgiev.com)
    Adapted Web By : Marlon Nardi - (http://jsontodelphi.com)
*******************************************************************************}


interface

uses Generics.Collections, Rest.Json;

type

TRootClass = class
private
  FFoo: Extended;
  FFoo: Extended;
public
  property Foo: Extended read FFoo write FFoo;
  property foo: Extended read FFoo write FFoo;
  function ToJsonString: string;
  class function FromJsonString(AJsonString: string): TRootClass;
end;

implementation

{TRootClass}


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.

Geändert von Schokohase ( 8. Okt 2018 um 05:21 Uhr)
  Mit Zitat antworten Zitat