Avatar of alpires
alpires
Flag for Brazil asked on

JSON whith Delphi Xe2

Hi Experts,

I have a JSON file with the following structure:

 level 1  
  1
  2
  3     level 2
  4------4.1
          4.2      level 3
          4.3------4.3.1
          4.4      4.3.2
           ...       4.3.3
                    4.3.4
                     .....

My file has 3 level of datas par. My delphi code only get information until level 2, but i need get all informationsof of all levels. Thanks for some help.

My code is:

procedure TForm1.inicio;
var
  obj1, obj2: TJSONObject;
  array1: TJSONArray;
  par1, par2: TJSONPair;
  i, j: integer;
  valor: string;
begin
  valor := Memo1.Text;
  obj1 := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(valor), 0)
    as TJSONObject;

  par1 := TJSONPair.Create;
  par1 := obj1.Get(3);

  array1 := TJSONArray.Create;
  array1 := (par1.JsonValue as TJSONArray);

  Form1.Memo2.Lines.Add('number of object is ' + IntToStr(array1.Size));

  obj2 := TJSONObject.Create;
  par2 := TJSONPair.Create;

  for i := 0 to array1.Size - 1 do
  begin
    obj2 := (array1.Get(i) as TJSONObject);
    Form1.Memo2.Lines.Add('');
    Form1.Memo2.Lines.Add('Element ' + IntToStr(i) +
      ' the number of pairs of the object is = ' + IntToStr(obj2.Size));
    for j := 0 to obj2.Size - 1 do
    begin
      par2 := obj2.Get(j);
      Form1.Memo2.Lines.Add(par2.JsonString.Value + ' : ' +
        par2.JsonValue.Value);
    end;
  end;
end;
DelphiPascalProgramming Languages-Other

Avatar of undefined
Last Comment
Sinisa Vuk

8/22/2022 - Mon
Sinisa Vuk

what is your goal? to fill memo or .... when you have tree like structure it is recommended
to use recursive functions (when you detect subtree/level then call function itself with some other parameters)
alpires

ASKER
Memo is only a example. I need get the informations and put in a var, but I dont know how do this using TJSONObject.
ASKER CERTIFIED SOLUTION
Sinisa Vuk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck