Solved

JSON whith Delphi Xe2

Posted on 2012-03-14
3
5,003 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 27

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 27

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

734 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