Appearance
Iterating Through TJSONObject Keys And Values
We have already seen an example how to iterate TJSONArray elements.
There are following methods and properties of TJSONObject available to loop through the keys and values of JSON objects:
Count – Returns the number of the Key-Values pairs in the JSON object.
Keys[i] – Returns the key value as a string of the pair with index i of the JSON object.
Values[i] – Returns TJSONValue of the pair with index i of the JSON object.
pascal
var
i: integer;
obj: TJSONObject;
begin
obj := TJSONValue.Parse('{"PM1": {"firstName": "John", "lastName": "Key"}, "PM2": {"firstName": "Jacinda", "lastName": "Ardern"}}') as TJSONObject;
for i := 0 to obj.Count - 1 do
LogInfo(obj.Keys[i] + ': ' + obj.Values[i]['firstName'].AsString + ' ' + obj.Values[i]['lastName'].AsString);
obj.Free;
end;Output
PM1: John Key
PM2: Jacinda Ardern

