<< Click to Display Table of Contents >> Navigation: JSON > Iterating |
THtJSON elements of object and array type supports iterators.
Example:
JS.Parse('{"result": [1, 2, 3]}');
for var E in JS['result'] do
v := v + E.AsInteger;
Node list returned by Find method (See JSON Path) also supports iteration.
For E in JS.Find('result.orders.*.product') do
..
Note that each item is fetched on-demand so there is no memory consumption when processing large sets and first element is returned quickly.
Iterator has the following methods:
Count: NativeInt; - number of elements
Sum: double; - sum of numeric values
Max: double - max numeric value
Min: double; - min numeric value
Last: PHtJSON - last element
List(const ASeparator: string): string - Combine values into single string
Template(const ATemplate: string; const ASeparator: string = ''): string - Combine elements using HTML template. Example:
JS.Find('result.orders.*').Template('<div class="order"><div class="id">{{id}}</div><div class="product">{{product.name}}></div></div>');
JSONTemplate(const ATemplate: string): string; Combine items using JSON template
Deserialize(AConstructor: THtDeserializeCreate); Convert to list of objects. Example:
JS.Find('result.orders.*').Deserialize(
function: TObject
begin
Result := TMyOrder.Create;
Orders.Add(Result);
end
);
TMyOrder properties and fields with the same names as JSON keys will be filled by JSON values.