Unit1

 avatar
unknown
pascal
2 years ago
1.9 kB
5
Indexable
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,System.Net.HttpClient,System.Net.HttpClientComponent,
  System.Json, Rest.Json,JsonDTO;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TMarket = class(Tobject)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  HttpClient: TNetHTTPClient;
  Response: IHTTPResponse;
  URL: string;
  I: Integer;
  Personx:TMarket;
  JsonValue:TJsonValue;
  JsonArray:TJSONArray;
  ResponseObject:TItem; // type is from the DTO class, where the json structure described as a Delphi Object
begin
    HttpClient := TNetHTTPClient.Create(self);
  try
    URL := 'https://api.canlidoviz.com/items/current?marketId=0&code=TRY&code=GA';
    Response := HttpClient.Get(URL);
    if Response.StatusCode = 200 then
    begin
      //the result is an array, so parse it
      JsonArray := TJSONObject.ParseJSONValue(Response.ContentAsString(TEncoding.UTF8)) as TJSONArray;

       // iterate trough the array, each element is a json object
       for JsonValue in JsonArray do
       begin
          //parse the json object
          ResponseObject:=TJson.JsonToObject<TItem>(JsonValue as TJsonObject);

          //do whatewer you want with the parsed object
          ShowMessage(
            ResponseObject.name + 'buys in market ' + ResponseObject.market.name
          );
       end;
    end
    else
    begin
      ShowMessage('Error: ' + Response.StatusCode.ToString);
    end;
  finally
    HttpClient.Free;
  end;
end;

end.