Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program TryGetValueSample;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils, System.JSON;
- var
- lStr: String;
- lJObj: TJSONObject;
- lJString: TJSONString;
- lJNumber: TJSONNumber;
- begin
- //lStr := '{"name":"Daniele","age":4}';
- lStr := '{"name":"Daniele"}';
- lJObj := TJSONObject.ParseJSONValue(lStr) as TJSONObject;
- try
- if lJObj.TryGetValue<TJSONString>('name', lJString) then
- begin
- WriteLn('NAME found: ' + lJString.Value);
- end
- else
- begin
- WriteLn('NAME not found');
- end;
- if lJObj.TryGetValue<TJSONNumber>('age', lJNumber) then
- begin
- WriteLn('AGE found: ' + lJNumber.Value);
- end
- else
- begin
- WriteLn('AGE not found');
- end;
- finally
- lJObj.Free;
- end;
- ReadLn;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement