Solved

JSon parser in Delphi

Posted on 2013-12-20
1
6,187 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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

772 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