Solved

JSon parser in Delphi

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…
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.

810 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