Solved

JSON whith Delphi Xe2

Posted on 2012-03-14
3
4,896 Views
Last Modified: 2012-08-13
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;
0
Comment
Question by:alpires
  • 2
3 Comments
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 37723619
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)
0
 

Author Comment

by:alpires
ID: 37724289
Memo is only a example. I need get the informations and put in a var, but I dont know how do this using TJSONObject.
0
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 37724304
Maybe you can look this:
http://edn.embarcadero.com/article/40882

Work wit Json is more like working with xml.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question