[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

JSon parser in Delphi

Posted on 2013-12-20
1
Medium Priority
?
8,691 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
[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
1 Comment
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 2000 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

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

656 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