Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

1 MONTH VIP - 5$; 3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

How to parse nested JSON object in Delphi XE2

Discussion in 'Delphi Components' started by N0body, Apr 19, 2013.

  1. N0body
    Online

    N0bodyN0body is a Verified Member DelphiFan Administrator Staff Member DF Staff

    Code:
    How to parse nested JSON object in Delphi XE2?
    {
        "products": {
            "Men's Sneakers": {
                "instock": false,
                "size": "423",
                "manufacturer": "Adidas",
                "lastcheck": "20120529"
            },
            "Purse": {
                "instock": true,
                "size": "not applicable",
                "manufacturer": "Prada",
                "lastcheck": "20120528"
            },
            "Men's Hood": {
                "instock": false,
                "size": "M",
                "manufacturer": "Generic",
                "lastcheck": "20120529"
           }
        },
       "total": 41,
       "available": 30
    }
           
    procedure TForm1.ParseString(const AString: string);
    var
      json          : TJSONObject;
      jPair         : TJSONPair;
      jValue        : TJSONValue;
      jcValue       : TJSONValue;
      l,i           : Integer;
    begin
        json    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(AString),0) as TJSONObject;
      try
        //get the pair to evaluate in this case the index is 1
        jPair   := json.Get(1);
            {further process the nested objects and adding them to the listview}
      finally
         json.Free;
      end;
    end;
           
    {$APPTYPE CONSOLE}
     
    {$R *.res}
     
    uses
      DBXJSON,
      System.SysUtils;
     
     
    Const
    StrJson=
    '{'+
    '    "products": {'+
    '        "Men''s Sneakers": {'+
    '            "instock": false,'+
    '            "size": "423",'+
    '            "manufacturer": "Adidas",'+
    '            "lastcheck": "20120529"'+
    '        },'+
    '        "Purse": {'+
    '            "instock": true,'+
    '            "size": "not applicable",'+
    '            "manufacturer": "Prada",'+
    '            "lastcheck": "20120528"'+
    '        },'+
    '        "Men''s Hood": {'+
    '            "instock": false,'+
    '            "size": "M",'+
    '            "manufacturer": "Generic",'+
    '            "lastcheck": "20120529"'+
    '        }'+
    '    },'+
    '    "total": 41,'+
    '    "available": 30'+
    '}';
     
    procedure ParseJson;
    var
      LJsonObj  : TJSONObject;
      LJPair    : TJSONPair;
      LProducts : TJSONValue;
      LProduct  : TJSONValue;
      LItem     : TJSONValue;
      LIndex    : Integer;
      LSize     : Integer;
    begin
        LJsonObj    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson),0) as TJSONObject;
      try
         LProducts:=LJsonObj.Get('products').JsonValue;
         LSize:=TJSONArray(LProducts).Size;
         for LIndex:=0 to LSize-1 do
         begin
          LProduct := TJSONArray(LProducts).Get(LIndex);
          LJPair   := TJSONPair(LProduct);
          Writeln(Format('Product Name %s',[LJPair.JsonString.Value]));
            for LItem in TJSONArray(LJPair.JsonValue) do
            begin
               if TJSONPair(LItem).JsonValue is TJSONFalse then
                Writeln(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, 'false']))
               else
               if TJSONPair(LItem).JsonValue is TJSONTrue then
                Writeln(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, 'true']))
               else
                Writeln(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, TJSONPair(LItem).JsonValue.Value]));
            end;
         end;
      finally
         LJsonObj.Free;
      end;
    end;
     
    begin
      try
        ParseJson;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
      Readln;
    end.
           
    Product Name Men's Sneakers
      instock : false
      size : 423
      manufacturer : Adidas
      lastcheck : 20120529
    Product Name Purse
      instock : true
      size : not applicable
      manufacturer : Prada
      lastcheck : 20120528
    Product Name Men's Hood
      instock : false
      size : M
      manufacturer : Generic
      lastcheck : 20120529
           
    products   := jPair.Get('products');
    purse      := products.GetJsonValue().Get('Purse');
    purseManuf := purse.GetJsonValue().Get('manufacturer');
    ...
           
    products   := jPair.Get('products');
    for prodPair in products.GetEnumerator() do
    begin
        prodName := prodPair.GetJsonString();
        prodObj  := prodPair.GetJsonValue();
        ...
     
  2. Netmod
    Offline

    Netmod DF Member

    thank you
     

Share This Page

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block