TJSONIterator = class
class PASCALIMPLEMENTATION TJSONIterator : public System::TObject
Allows you to iterate through the content that a JSON reader provides.
Iterators are similar to JSON reader, but they provide some additional features, and behave slightly differently. For example:
- Iterators do not need to go through all tokens. You can advance to specific array or object items, skip parts of arrays and objects, or skip whole arrays and objects.
- When an iterator is at the token of an array or object value, you can read the array index or the object key that matches that value. You do not have to keep track of that information separately.
- Iterators do not stop at object keys. Instead, iterators skip keys and iterate to the token of their corresponding value. You can read the key of a value token from the Key property of the iterator.
Iterating Through the Input Data
To iterate through the contents of the source JSON reader:
- Create a JSON iterator that reads data from a JSON reader.
- Call Next so that the reader moves to the first JSON token of the input data.
- Type indicates the type of the token.
- If you are iterating an array, use Index to obtain the index of the current token. If you are iterating an object, use Key to obtain the key of the current token.
- Use one of the value properties of the iterator to access the value of the current token. The type of a token determines the type of its value, which determines the value property that you must use to access the value of the token. For example, if Type is
Boolean, you can call AsBoolean to access the boolean value of the current token.
- Keep calling Next to go through all the JSON tokens of the input data. Next returns
Falsewhen it reaches the end of the input data.
You can use Rewind to move the iterator back to the beginning of the input data.
TJSONIterator provides several properties that you can read to obtain the value of the current token of the iterator. The value of the Type property determines which properties you can use to read the value.
TJSONIterator is a shallow iterator by default. Every time that the iterator reaches a token of type
TJsonToken.StartObject, you must call Recurse before your next call to Next to ensure that the iterator does not skip the whole array or object.
During recursion, you can use Return to skip the remaining content of the current array or object, and returns the iterator back to the parent of the array or object.
When you start iterating the source JSON reader, Depth is 0. Each time that you use recursion to go into an array or an object, the depth increases by 1. When you leave an array or object, either because you have reached its end or because you skipped to its end using Return, the depth decreases by 1. InRecurse indicates whether or not the iterator is in a depth higher than 0.