Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   einfaches JSON Beispiel (https://www.delphipraxis.net/210189-einfaches-json-beispiel.html)

fabi17 15. Mär 2022 16:01

einfaches JSON Beispiel
 
Hallo zusammen,

stehe mit JSON auf dem Kriegsuß, kann mir jemand bitte ein simples Beispiel für die Abfrage vom 2. "status" Feld geben? danke vorab!

Code:
{"status":"success","data":{"status":"online","benchmarking":"TEST","online":"connected"}}
danke!:thumb:

DeddyH 16. Mär 2022 05:51

AW: einfaches JSON Beispiel
 
Ohne jede Fehlerbehandlung:
Delphi-Quellcode:
uses System.JSON;

const _JSON = '{"status":"success","data":{"status":"online","benchmarking":"TEST","online":"connected"}}';

procedure TFormTest.btnTestClick(Sender: TObject);
var
  lOuterObj: TJSONObject;
  lInnerObj: TJSONObject;
begin
  lOuterObj := TJSONObject.ParseJSONValue(_JSON) as TJSONObject;
  try
    lInnerObj := lOuterObj.Values['data'] as TJSONObject;
    ShowMessage(lInnerObj.Values['status'].Value);
  finally
    lOuterObj.Free;
  end;
end;

Der schöne Günther 16. Mär 2022 07:10

AW: einfaches JSON Beispiel
 
Alternative:

Delphi-Quellcode:
program Project1;

{$R *.res}

uses System.SysUtils, System.JSON;

const
   input = '{"status":"success","data":{"status":"online","benchmarking":"TEST","online":"connected"}}';
var
   jsonObj: TJsonObject;
   status: String;
begin
   jsonObj := TJSONObject.ParseJSONValue(input) as TJsonObject;
   if(not Assigned(jsonObj)) then
      WriteLn('input is no valid json object')
   else
      try
         status := jsonObj.GetValue<String>('data.status');
         WriteLn('status is ', status);
      finally
         jsonObj.Destroy();
      end;
end.

fabi17 16. Mär 2022 10:39

AW: einfaches JSON Beispiel
 
Zitat:

Zitat von Der schöne Günther (Beitrag 1503377)
Alternative:

Delphi-Quellcode:
program Project1;

{$R *.res}

uses System.SysUtils, System.JSON;

const
   input = '{"status":"success","data":{"status":"online","benchmarking":"TEST","online":"connected"}}';
var
   jsonObj: TJsonObject;
   status: String;
begin
   jsonObj := TJSONObject.ParseJSONValue(input) as TJsonObject;
   if(not Assigned(jsonObj)) then
      WriteLn('input is no valid json object')
   else
      try
         status := jsonObj.GetValue<String>('data.status');
         WriteLn('status is ', status);
      finally
         jsonObj.Destroy();
      end;
end.


ahhh, das "data.status" hatte ich nicht so....


Danke an beide! So langsam begreife ich es mal...


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:33 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz