Solved

JSON whith Delphi Xe2

Posted on 2012-03-14
3
4,867 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Mdi application Child forms get behind control 7 151
mapAB Challlenge 35 123
Firemonkey webbrowser scrollbars ? 1 29
Advice in Xamarin 21 48
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now