Solved

JSon parser in Delphi

Posted on 2013-12-20
1
6,487 Views
Last Modified: 2013-12-20
Hi Experts,

I have a code in delphi xe4 to parser jsons:

var
  jsonObj, jSubObj: TJSONObject;
  ja: TJSONArray;
  jp, jSubPar: TJSONPair;
  pagina: string;
  i, j: integer;
begin
  pagina := Memo1.Text;
  jsonObj := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(pagina), 0)
    as TJSONObject;

  jp := TJSONPair.create;
  jp := jsonObj.Get(0);

  ja := TJSONArray.create;
  ja := (jp.JsonValue as TJSONArray);

  Form1.Memo2.Lines.Add('number of elements ' + inttostr(ja.Size));
  jSubObj := TJSONObject.create;
  jSubPar := TJSONPair.create;

  for i := 0 to ja.Size - 1 do
  begin
    jSubObj := (ja.Get(i) as TJSONObject);

    Form1.Memo2.Lines.Add('');
    Form1.Memo2.Lines.Add('Element number ' + inttostr(i) +
      ' the number of pairs is = ' + inttostr(jSubObj.Size));

    for j := 0 to jSubObj.Size - 1 do
    begin
      jSubPar := jSubObj.Get(j);
      Form1.Memo2.Lines.Add(jSubPar.JsonString.Value + ' : ' +
        jSubPar.JsonValue.Value);
    end;

  end;


I'm trying to parser values ¿¿returned by facebook api, like this:

{"data":[{"uid":100005219556303,"name":"Otavio Enrico","username":"otavio.enrico.1","sex":"male","current_location":{"city":"Mogi das Cruzes","state":"S\u00e3o Paulo","country":"Brazil","zip":"","latitude":-23.5167,"longitude":-46.1833,"id":103427389698118,"name":"Mogi das Cruzes"},"hometown_location":{"city":"Mogi das Cruzes","state":"S\u00e3o Paulo","country":"Brazil","zip":"","latitude":-23.5167,"longitude":-46.1833,"id":103427389698118,"name":"Mogi das Cruzes"},"religion":null,"relationship_status":null},{"uid":100005969143030,"name":"Nivaldo Lucas","username":"nivaldo.lucas.56","sex":"male","current_location":null,"hometown_location":null,"religion":null,"relationship_status":null}]}

But my code dont get the values in level 2 like "city", "state", "latitude" and "longitude".
Somebody could help me change my code to get these values¿¿?

Thanks
0
Comment
Question by:alpires
1 Comment
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 500 total points
ID: 39731787
For nested JSON string you will need recursion.

I have put an example together for you. There is something wrong as the hometown_location is parsed twice. Unfortunately I have run out of time for now, I will have a look at it again later to see if I can spot the problem.

When running the code, I get the following result:
{
data
  [
    {
    uid : 100005219556303
    name : Otavio Enrico
    username : otavio.enrico.1
    sex : male
    current_location
      {
      city : Mogi das Cruzes
      state : São Paulo
      country : Brazil
      zip : 
      latitude : -23.5167
      longitude : -46.1833
      id : 103427389698118
      name : Mogi das Cruzes
      }
    hometown_location
      {
      city : Mogi das Cruzes
      state : São Paulo
      country : Brazil
      zip : 
      latitude : -23.5167
      longitude : -46.1833
      id : 103427389698118
      name : Mogi das Cruzes
      }
      {
      city : Mogi das Cruzes
      state : São Paulo
      country : Brazil
      zip : 
      latitude : -23.5167
      longitude : -46.1833
      id : 103427389698118
      name : Mogi das Cruzes
      }
    religion : null
    relationship_status : null
    }
    {
    uid : 100005969143030
    name : Nivaldo Lucas
    username : nivaldo.lucas.56
    sex : male
    current_location : null
    hometown_location : null
    religion : null
    relationship_status : null
    }
  ]
}

Open in new window


here it is:
function ParseArray(ja: TJSONArray; var indent: integer):string;
var ai: integer;
    v: TJSONValue;
begin
  inc(indent);
  result:= result + StringOfChar(' ', indent*2)+'['#13#10;
  for ai := 0 to ja.Size-1 do
  begin
    v:= ja.Get(ai);
    if v is TJSONArray then result:= parseArray(v as TJSONArray, indent)
    else if v is TJSONObject then result:= ParseObject(v as TJSONObject, indent);
  end;
  result:= result + StringOfChar(' ', indent*2)+']'#13#10;
  dec(indent);
end;

function ParseObject(jo: tjsonObject; var indent: integer):string;
var oi: integer;
    v: TJSONPair;
begin
  inc(indent);
  result:= result + StringOfChar(' ', indent*2)+'{'#13#10;
  for oi := 0 to jo.Size-1 do
  begin
    v:= jo.Get(oi);
    if v.JsonValue is TJSONArray  then result:= result + StringOfChar(' ', indent*2)+ v.JsonString.Value +#13#10+parseArray(v.JsonValue as TJSONArray, indent)
    else if v.JsonValue is TJSONObject then result:= result + StringOfChar(' ', indent*2)+ v.JsonString.Value +#13#10+ParseObject(v.JsonValue as TJSONObject, indent)
    else if v.JsonValue is TJSONString then result:= result + StringOfChar(' ', indent*2)+ v.JsonString.Value +' : '+ v.JsonValue.Value+#13#10
    else if v.JsonValue is TJSONNull   then result:= result + StringOfChar(' ', indent*2)+ v.JsonString.Value +' : null'#13#10;

  end;
  result:= result + StringOfChar(' ', indent*2)+'}'#13#10;
  dec(indent);
end;

function ParseJSON(s: string):string;
var o: TJSONObject;
    indent: integer;
begin
  indent:= -1;
  o:= TJSONObject.ParseJSONValue(s) as TJSONObject;
  try
    result:= ParseObject(o, indent);
  finally
    o.Free;
  end;
end;

Open in new window


e.g. use would be :
yourmemo.text := ParseJSON('YourJSON string');
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
mapShare challenge 13 91
groovy example issue 10 90
How to build JSON File in Delphi 6 3 17
CONDITIONAL MAX STATEMENT IN T-SQL 3 41
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

24 Experts available now in Live!

Get 1:1 Help Now