Unit 'fpjson' Package
[Overview][Constants][Types][Classes][Procedures and functions][Index] [#fcl]

TJSONData.FindPath

Find data by name.

Declaration

Source position: fpjson.pp line 174

public function TJSONData.FindPath(

  const APath: TJSONStringType

):TJSONData;

Arguments

APath

  

Path to search for.

Function result

The found data element, or nil.

Description

FindPath finds a value based on its path. If none is found, Nil is returned. The path elements are separated by dots and square brackets, as in object member notation or array notation. The path is case sensitive.

The following code will return the value itself, i.e. E will contain the same element as D:

Var
  D,E : TJSONData;

begin
  D:=TJSONIntegerNumber.Create(123);
  E:=D.FindPath('');
end.

The following code will not return anything:

Var
  D,E : TJSONData;

begin
  D:=TJSONIntegerNumber.Create(123);
  E:=D.FindPath('a');
end.

The following code will return the third element from the array:

Var
  D,E : TJSONData;

begin
  D:=TJSONArray.Create([1,2,3,4,5]);
  E:=D.FindPath('[2]');
  Writeln(E.AsJSON);
end.

The output of this program is 3.

The following code returns the element Age from the object:

Var
  D,E : TJSONData;

begin
  D:=TJSONObject.Create(['Age',23,
                         'Lastame','Rodriguez',
                         'FirstName','Roberto']);
  E:=D.FindPath('Age');
  Writeln(E.AsJSON);
end.

The code will print 23.

Obviously, this can be combined:

Var
  D,E : TJSONData;

begin
  D:=TJSONObject.Create(['Age',23,
                         'Names', TJSONObject.Create([
                             'LastName','Rodriguez',
                             'FirstName','Roberto'])]);
  E:=D.FindPath('Names.LastName');
  Writeln(E.AsJSON);
end.

And mixed:

var
  D,E : TJSONData;

begin
  D:=TJSONObject.Create(['Children',
       TJSONArray.Create([
         TJSONObject.Create(['Age',23,
                             'Names', TJSONObject.Create([
                               'LastName','Rodriguez',
                               'FirstName','Roberto'])
         ]),
         TJSONObject.Create(['Age',20,
                             'Names', TJSONObject.Create([
                                   'LastName','Rodriguez',
                                   'FirstName','Maria'])
        ])
      ])
     ]);
  E:=D.FindPath('Children[1].Names.FirstName');
  Writeln(E.AsJSON);
end.

See also

TJSONArray

  

Class of TJSONArray.

TJSONObject

  

Class of TJSONObject.

GetPath

  

Get data by name.


Documentation generated on: Nov 22 2024