RTL.JSONIterator

From RAD Studio Code Examples
Jump to: navigation, search

Description

This code snippet shows how to create and use a JSON Iterator using the System.JSON.Builders framework.

Code

The following JSON object contains information about purchase records from a website.

{"Transaction":[ 
  {"id":662713, "firstName":"John", "lastName":"Doe", "price": 2.1, "parent_id": null, validated:true},
  {"id":662714, "firstName":"Anna", "lastName":"Smith", "price": 4.5, "parent_id": null, validated: false}, 
  {"id":662715, "firstName":"Peter", "lastName":"Jones", "price": 3.6, "parent_id": null, validated: true} 
  ]}

The sample consists of a TMemo that shows the result of parsing the JSON object. The sample iterates through the JSON reading the content and displaying certain information in the TMemo.

The sample moves through the JSON data and displays the name of the array as well as certain key and value pairs.

  • The sample uses Recurse to go down one level in the JSON structure to enter an object or an array.
  • The Return method is used to go up one level in the JSON structure to return to the parent array or object.
  • The Next method allows moving through the JSON data; either one step forward to next item, or to a particular item passed through the AKey parameter.
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure ParseObject;
  end;

var
  Form1: TForm1;

implementation

uses   System.JSON.Builders, System.JSON.Readers;

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ParseObject;
end;

Procedure TForm1.ParseObject;
var
  LIterator: TJSONIterator;
  LJsonTextReader: TJsonTextReader;
  LStringReader: TStringReader;
begin
  LStringReader := TStringReader.Create('{"Transaction":[' + sLineBreak
    + '{"id":662713, "firstName":"John", "lastName":"Doe", "price": 2.1, "parent_id": null, validated:true}, ' + sLineBreak
    + '{"id":662714, "firstName":"Anna", "lastName":"Smith", "price": 4.5, "parent_id": null, validated: false},' + sLineBreak
    + '{"id":662715, "firstName":"Peter", "lastName":"Jones", "price": 3.6, "parent_id": null, validated: true} ' + sLineBreak
    + ']}');

  LJsonTextReader := TJsonTextReader.Create(LStringReader);
  LIterator := TJSONIterator.Create(LJsonTextReader);

  LIterator.Recurse; // prepare to enter array
  LIterator.Next;
  LIterator.Recurse;  // enter array
  Memo1.Lines.Add(LIterator.Key); // Transaction
  LIterator.Recurse; // prepare to enter object
  LIterator.Next;
  LIterator.Recurse; // enter array object
  LIterator.Next;
  Memo1.Lines.Add(LIterator.Key +': '+ IntToStr(LIterator.AsInteger)); // id : 662713
  LIterator.Next;
  Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); //  firstName : John
  LIterator.Next;
  Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); // lastName : Doe
  LIterator.Next;
  Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsDouble.ToString);   // price : 2.1
  LIterator.Next;
  if LIterator.IsNull then
    Memo1.Lines.Add(LIterator.Key +': null');  // parent_id : null
  LIterator.Next;
  if LIterator.AsBoolean then
    Memo1.Lines.Add(LIterator.Key +': true') // validated : true
  else
    Memo1.Lines.Add(LIterator.Key +': false');
  LIterator.Return; // back to parent structure
  LIterator.Recurse; // prepare to enter object
  LIterator.Next;
  LIterator.Recurse; // enter object
  LIterator.Next;
  Memo1.Lines.Add(LIterator.Key +': '+ IntToStr(LIterator.AsInteger)); // id : 662714
  LIterator.Next('price');  // move to key 'price'
  Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsDouble.ToString); // price : 4.5
  LIterator.Next;
  if LIterator.IsNull then
  Memo1.Lines.Add(LIterator.Key +': null');  // parent_id : null
  LIterator.Return;  // back to parent structure
  LIterator.Recurse;  // prepare to enter object
  LIterator.Next;
  LIterator.Recurse; // enter object
  LIterator.Next;
  LIterator.Next('firstName'); // move to key 'firstName'
  Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); // firstName : Peter
  LIterator.Next;
  Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); // lastName : Jones
end;

When you run the sample and click the button, this is the output of the memo:

Transaction id: 662713 firstName: John lastName: Doe price: 2.1 parent_id: null validated: true id: 662714 price: 4.5 parent_id: null firstName: Peter lastName: Jones

Uses

See Also