Created
November 9, 2021 10:39
-
-
Save UweRaabe/387519566ef7a76e7d2140bcf526ddc6 to your computer and use it in GitHub Desktop.
Convert JSON to object structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program MyJsonDemo; | |
{$APPTYPE CONSOLE} | |
uses | |
System.SysUtils, | |
REST.Json, | |
MyJsonTypes in 'MyJsonTypes.pas'; | |
const | |
cJson = '{' + | |
'"ablageX": {' + | |
'"ablageId": 81,' + | |
'"lsIdx": 1' + | |
'},' + | |
'"prozessAblaufX": [' + | |
'{' + | |
'"id": 1,' + | |
'"typ": "TRolleundRollenbahn",' + | |
'"strgVariante": "svH",' + | |
'"wait": true' + | |
'},' + | |
'{' + | |
'"id": 2,' + | |
'"typ": "TAusstosserB",' + | |
'"strgVariante": "svE",' + | |
'"wait": true' + | |
'},' + | |
'{' + | |
'"id": 3,' + | |
'"typ": "Querfoerderer1",' + | |
'"strgVariante": "svB",' + | |
'"wait": false' + | |
'}' + | |
']' + | |
'}'; | |
var | |
config: TConfig; | |
begin | |
try | |
config := TJson.JsonToObject<TConfig>(cJson); | |
try | |
{ hier stehen alle Konfigurationsdaten in config } | |
finally | |
config.Free; | |
end; | |
except | |
on E: Exception do | |
Writeln(E.ClassName, ': ', E.Message); | |
end; | |
end. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit MyJsonTypes; | |
interface | |
type | |
TAblage = class | |
private | |
FAblageId: Integer; | |
FlsIdx: Integer; | |
public | |
property AblageId: Integer read FAblageId write FAblageId; | |
property lsIdx: Integer read FlsIdx write FlsIdx; | |
end; | |
TProzessAblauf = class | |
private | |
Fid: Integer; | |
FstrgVariante: string; | |
Ftyp: string; | |
Fwait: Boolean; | |
public | |
property id: Integer read Fid write Fid; | |
property strgVariante: string read FstrgVariante write FstrgVariante; | |
property typ: string read Ftyp write Ftyp; | |
property wait: Boolean read Fwait write Fwait; | |
end; | |
TConfig = class | |
private | |
FAblageX: TAblage; | |
FProzessAblaufX: TArray<TProzessAblauf>; | |
public | |
constructor Create; | |
destructor Destroy; override; | |
property AblageX: TAblage read FAblageX; | |
property ProzessAblaufX: TArray<TProzessAblauf> read FProzessAblaufX; | |
end; | |
implementation | |
constructor TConfig.Create; | |
begin | |
inherited Create; | |
end; | |
destructor TConfig.Destroy; | |
var | |
item: TProzessAblauf; | |
begin | |
FAblageX.Free; | |
for item in FProzessAblaufX do | |
item.Free; | |
inherited Destroy; | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment