Advertisement
danieleteti

TJSONObject.TryGetValue

Apr 17th, 2016
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.78 KB | None | 0 0
  1. program TryGetValueSample;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.   System.SysUtils, System.JSON;
  8.  
  9. var
  10.   lStr: String;
  11.   lJObj: TJSONObject;
  12.   lJString: TJSONString;
  13.   lJNumber: TJSONNumber;
  14.  
  15. begin
  16.   //lStr := '{"name":"Daniele","age":4}';
  17.  
  18.   lStr := '{"name":"Daniele"}';
  19.  
  20.   lJObj := TJSONObject.ParseJSONValue(lStr) as TJSONObject;
  21.   try
  22.     if lJObj.TryGetValue<TJSONString>('name', lJString) then
  23.     begin
  24.       WriteLn('NAME found: ' + lJString.Value);
  25.     end
  26.     else
  27.     begin
  28.       WriteLn('NAME not found');
  29.     end;
  30.     if lJObj.TryGetValue<TJSONNumber>('age', lJNumber) then
  31.     begin
  32.       WriteLn('AGE found: ' + lJNumber.Value);
  33.     end
  34.     else
  35.     begin
  36.       WriteLn('AGE not found');
  37.     end;
  38.  
  39.   finally
  40.     lJObj.Free;
  41.   end;
  42.   ReadLn;
  43.  
  44. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement