Solved

JSON whith Delphi Xe2

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

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 25

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 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

11 Experts available now in Live!

Get 1:1 Help Now