AGB  ·  Datenschutz  ·  Impressum  







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

einfaches JSON Beispiel

Ein Thema von fabi17 · begonnen am 15. Mär 2022 · letzter Beitrag vom 16. Mär 2022
Antwort Antwort
fabi17

Registriert seit: 1. Mai 2017
44 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#1

einfaches JSON Beispiel

  Alt 15. Mär 2022, 16:01
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!
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.541 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: einfaches JSON Beispiel

  Alt 16. Mär 2022, 05:51
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;
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Der schöne Günther

Registriert seit: 6. Mär 2013
6.110 Beiträge
 
Delphi 10 Seattle Enterprise
 
#3

AW: einfaches JSON Beispiel

  Alt 16. Mär 2022, 07:10
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.
  Mit Zitat antworten Zitat
fabi17

Registriert seit: 1. Mai 2017
44 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#4

AW: einfaches JSON Beispiel

  Alt 16. Mär 2022, 10:39
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...
  Mit Zitat antworten Zitat
Antwort Antwort

 

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 19:04 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