Einzelnen Beitrag anzeigen

LoZe

Registriert seit: 27. Dez 2016
Ort: Ulm
38 Beiträge
 
Delphi 12 Athens
 
#11

AW: JSON iterieren, aber wie?

  Alt 27. Sep 2022, 08:42
mach doch einfach ne Klasse von dem Json dann kannst du einfach aus der Klasse die Informationen auslesen.

https://jsontodelphi.com/

Der Aufruf
Delphi-Quellcode:
uses RootUnit;

procedure UseJson(pJson:string);
var
  Root: TRoot;
  Data: TData;
begin
  Root := TRoot.Create;
  try
    Root.AsJson := pJson;
    // in Root.Data sind alle Items aus dem Array drin
    for Data in Root.Data do
    begin
      Data.Article....
    end;

  finally
    Root.Free;
  end;
end;
Die Klasse
Delphi-Quellcode:
unit RootUnit;

interface

uses
  Pkg.Json.DTO, System.Generics.Collections, REST.Json.Types;
{$M+}

type
  TArticle = class;
  TDimensions = class;
  TImportantInformation = class;
  TLinks = class;
  TPagination = class;
  TUnit = class;

  TLinks = class
  end;
  
  TPagination = class
  private
    FCount: Integer;
    [JSONName('current_page')]
    FCurrentPage: Integer;
    FLinks: TLinks;
    [JSONName('per_page')]
    FPerPage: Integer;
    FTotal: Integer;
    [JSONName('total_pages')]
    FTotalPages: Integer;
  published
    property Count: Integer read FCount write FCount;
    property CurrentPage: Integer read FCurrentPage write FCurrentPage;
    property Links: TLinks read FLinks;
    property PerPage: Integer read FPerPage write FPerPage;
    property Total: Integer read FTotal write FTotal;
    property TotalPages: Integer read FTotalPages write FTotalPages;
  public
    constructor Create;
    destructor Destroy; override;
  end;
  
  TMeta = class
  private
    FPagination: TPagination;
  published
    property Pagination: TPagination read FPagination;
  public
    constructor Create;
    destructor Destroy; override;
  end;
  
  TImportantInformation = class
  end;
  
  TDimensions = class(TJsonDTO)
  private
    [JSONName('height')]
    FHeightArray: TArray<Integer>;
    [JSONMarshalled(False)]
    FHeight: TList<Integer>;
    [JSONName('length')]
    FLengthArray: TArray<Integer>;
    [JSONMarshalled(False)]
    FLength: TList<Integer>;
    [JSONName('width')]
    FWidthArray: TArray<Integer>;
    [JSONMarshalled(False)]
    FWidth: TList<Integer>;
    function GetHeight: TList<Integer>;
    function GetLength: TList<Integer>;
    function GetWidth: TList<Integer>;
  protected
    function GetAsJson: string; override;
  published
    property Height: TList<Integer> read GetHeight;
    property Length: TList<Integer> read GetLength;
    property Width: TList<Integer> read GetWidth;
  public
    destructor Destroy; override;
  end;
  
  TUnit = class
  private
    FAmount: Double;
    FUnit: string;
  published
    property Amount: Double read FAmount write FAmount;
    property &Unit: string read FUnit write FUnit;
  end;
  
  TArticle = class
  private
    [JSONName('created_at')]
    FCreatedAt: string;
    FDimensions: TDimensions;
    FId: Integer;
    [JSONName('important_information')]
    FImportantInformation: TImportantInformation;
    FManufacturer: string;
    FName: string;
    FPrice: Double;
    FQuality: string;
    FSku: string;
    FStock: Integer;
    FUnit: TUnit;
    FWeight: Double;
  published
    property CreatedAt: string read FCreatedAt write FCreatedAt;
    property Dimensions: TDimensions read FDimensions;
    property Id: Integer read FId write FId;
    property ImportantInformation: TImportantInformation read FImportantInformation;
    property Manufacturer: string read FManufacturer write FManufacturer;
    property Name: string read FName write FName;
    property Price: Double read FPrice write FPrice;
    property Quality: string read FQuality write FQuality;
    property Sku: string read FSku write FSku;
    property Stock: Integer read FStock write FStock;
    property &Unit: TUnit read FUnit;
    property Weight: Double read FWeight write FWeight;
  public
    constructor Create;
    destructor Destroy; override;
  end;
  
  TData = class
  private
    FArticle: TArticle;
  published
    property Article: TArticle read FArticle;
  public
    constructor Create;
    destructor Destroy; override;
  end;
  
  TRoot = class(TJsonDTO)
  private
    [JSONName('data'), JSONMarshalled(False)]
    FDataArray: TArray<TData>;
    [GenericListReflect]
    FData: TObjectList<TData>;
    FMeta: TMeta;
    function GetData: TObjectList<TData>;
  protected
    function GetAsJson: string; override;
  published
    property Data: TObjectList<TData> read GetData;
    property Meta: TMeta read FMeta;
  public
    constructor Create; override;
    destructor Destroy; override;
  end;
  
implementation

{ TPagination }

constructor TPagination.Create;
begin
  inherited;
  FLinks := TLinks.Create;
end;

destructor TPagination.Destroy;
begin
  FLinks.Free;
  inherited;
end;

{ TMeta }

constructor TMeta.Create;
begin
  inherited;
  FPagination := TPagination.Create;
end;

destructor TMeta.Destroy;
begin
  FPagination.Free;
  inherited;
end;

{ TDimensions }

destructor TDimensions.Destroy;
begin
  GetHeight.Free;
  GetLength.Free;
  GetWidth.Free;
  inherited;
end;

function TDimensions.GetHeight: TList<Integer>;
begin
  Result := List<Integer>(FHeight, FHeightArray);
end;

function TDimensions.GetLength: TList<Integer>;
begin
  Result := List<Integer>(FLength, FLengthArray);
end;

function TDimensions.GetWidth: TList<Integer>;
begin
  Result := List<Integer>(FWidth, FWidthArray);
end;

function TDimensions.GetAsJson: string;
begin
  RefreshArray<Integer>(FHeight, FHeightArray);
  RefreshArray<Integer>(FLength, FLengthArray);
  RefreshArray<Integer>(FWidth, FWidthArray);
  Result := inherited;
end;

{ TArticle }

constructor TArticle.Create;
begin
  inherited;
  FUnit := TUnit.Create;
  FDimensions := TDimensions.Create;
  FImportantInformation := TImportantInformation.Create;
end;

destructor TArticle.Destroy;
begin
  FUnit.Free;
  FDimensions.Free;
  FImportantInformation.Free;
  inherited;
end;

{ TData }

constructor TData.Create;
begin
  inherited;
  FArticle := TArticle.Create;
end;

destructor TData.Destroy;
begin
  FArticle.Free;
  inherited;
end;

{ TRoot }

constructor TRoot.Create;
begin
  inherited;
  FMeta := TMeta.Create;
end;

destructor TRoot.Destroy;
begin
  FMeta.Free;
  GetData.Free;
  inherited;
end;

function TRoot.GetData: TObjectList<TData>;
begin
  Result := ObjectList<TData>(FData, FDataArray);
end;

function TRoot.GetAsJson: string;
begin
  RefreshArray<TData>(FData, FDataArray);
  Result := inherited;
end;

end.
Chris

Geändert von LoZe (27. Sep 2022 um 08:47 Uhr)
  Mit Zitat antworten Zitat