AGB  ·  Datenschutz  ·  Impressum  







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

JSON iterieren, aber wie?

Ein Thema von stifflersmom · begonnen am 24. Jun 2022 · letzter Beitrag vom 27. Sep 2022
Antwort Antwort
Seite 2 von 2     12   
LoZe

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

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
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.372 Beiträge
 
Delphi 12 Athens
 
#2

AW: JSON iterieren, aber wie?

  Alt 27. Sep 2022, 09:38
Zitat:
Delphi-Quellcode:
    [JSONName('height')]
    FHeightArray: TArray<Integer>;
@LoZe: du mußt aufpassen, denn es sind keine "einheitlichen" Arrays.

also kein "length": [ 0, 1, 2 ], sondern jeweils Wert und Einheit ala "length": [ 0, "mm" ], statt als Array wäre es wohl in zwei Feldern angebrachter
Length: Integer;
LengthUnit: string; // oder Enum

oder ein nur Feld und beim Lesen in eine Grundeinheit umgerechnet
Ein Therapeut entspricht 1024 Gigapeut.

Geändert von himitsu (27. Sep 2022 um 09:41 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 2 von 2     12   


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 11:23 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-2025 by Thomas Breitkreuz