System.JSON.Builders.TJSONIterator
Delphi
TJSONIterator = class
C++
class PASCALIMPLEMENTATION TJSONIterator : public System::TObject
Contents
Properties
Type | Visibility | Source | Unit | Parent |
---|---|---|---|---|
class | public | System.JSON.Builders.pas System.JSON.Builders.hpp |
System.JSON.Builders | System.JSON.Builders |
Description
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
False
when it reaches the end of the input data.
You can use Rewind to move the iterator back to the beginning of the input data.
Value Properties
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.
The following table shows some values of the Type property and properties of TJSONIterator that you can use to read the value of the current token:
Type Value | TJSONIterator Properties |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AsString |
|
To access the value of the current token in a different format, you may use AsValue or AsVariant.
Using Recursion
TJSONIterator is a shallow iterator by default. Every time that the iterator reaches a token of type TJsonToken.StartObject
or 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.
ParentType indicates the type of the parent of the current token, either TJsonToken.StartObject
or TJsonToken.StartObject
. If you are not in any depth, ParentType is TJsonToken.None
.
Checking if Specific Data Exists
Use Find to determine if the content of the source JSON reader contains data matching a given JSON path.