• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9176
  • Last Modified:

JSon parser in Delphi

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
alpires
Asked:
alpires
1 Solution
 
Pierre CorneliusCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now