Einzelnen Beitrag anzeigen

Der schöne Günther

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

AW: TJsonValue mit Punkt im ValueNamen

  Alt 2. Sep 2021, 13:10
Was heißt für dich "knallen"?
GetValue() liefert nil , wenn es nichts hat. Das muss man prüfen, und kann nicht direkt sagen myJsonObject.GetValue('some.value').ToString() .

Hier ein Beispiel, das liefert folgendes, ohne jegliche Abstürze:

Code:
not found
Found: "Zwei"
not found
Delphi-Quellcode:
program Project1;

{$APPTYPE CONSOLE}

uses System.JSON;

procedure printValue(const input: String);
var
   asJsonObject: TJsonObject;
   someValue: TJsonValue;
begin
   asJsonObject := TJsonObject.ParseJSONValue(input) as TJsonObject;
   try
      someValue := asJsonObject.GetValue('some.value');
      if(Assigned(someValue)) then
         WriteLn('Found: ', someValue.ToString())
      else
         WriteLn('not found');
   finally
      asJsonObject.Destroy();
   end;
end;

procedure p();
const
   input1 = '{"some": {"value": "Eins"}}';
   input2 = '{"some.value": "Zwei"}';
   input3 = '{"some": {}}';
begin
   printValue(input1);
   printValue(input2);
   printValue(input3);
end;

begin
   p();
   ReadLn;
end.
  Mit Zitat antworten Zitat