How to do Json readings with multiple lists (DELPHI)?

2

I'm using the XSUPEROBJET examples to read Json below, but the examples use simple Json structures, and they work, but when I try to read the structures with multiple lists they do not work.

I do not know what I'm doing wrong.

My problem is: read these logs

    {
     "dadospedido":
     {
     "pedido":
       [
       {
         "ID":"19",
         "ID_EMPRESA":"107868",
         "DATA":"29/06/17",
         "ID_CLIENTE":"5",
         "ID_PAGTO":"29",
         "CODCLIENTE":"7",
         "HORA":"17:07:35",
         "CODVENDEDOR":"2",
         "CONLUIU":"C",
         "OBS":"Legal",
         "TOTAL":"60"
       }
       ],
     "itens":
       [
       {
         "ID":"65",
         "ID_PEDIDO":"19",
         "ID_EMPRESA":"107868",
         "ID_PRODUTO":"441200",
         "CODIGOPRODUTO":"506",
         "QTD":"1",
         "UNITARIO":"60",
         "UNIDADE":"UN",
         "VTOTAL":"60",
         "DESCRICAO":"Enviar em uma caixa com vários pra economizar"
       }
       ],
     "formapagto":
       [
       {
         "ID":"103",
         "ID_PEDIDO":"19",
         "ID_EMPRESA":"107868",
         "FORMA":"Boleto",
         "VENCIMENTO":"22/09/17",
         "VALOR":"10",
         "NOMEBANCO":"",
         "NOMECHEQUE":"",
         "NUMBANCO":"",
         "AGENCIA":"",
         "NUMCONTA":"",
         "NUMCHEQUE":"",
         "DATAATU":"30/12/99",
         "PARCELA":"1 - 6"
       },
       {
         "ID":"104",
         "ID_PEDIDO":"19",
         "ID_EMPRESA":"107868",
         "FORMA":"Boleto",
         "VENCIMENTO":"29/09/17",
         "VALOR":"10",
         "NOMEBANCO":"",
         "NOMECHEQUE":"",
         "NUMBANCO":"",
         "AGENCIA":"",
         "NUMCONTA":"",
         "NUMCHEQUE":"",
         "DATAATU":"30/12/99",
         "PARCELA":"2 - 6"
       },
       {
         "ID":"105",
         "ID_PEDIDO":"19",
         "ID_EMPRESA":"107868",
         "FORMA":"Boleto",
         "VENCIMENTO":"06/10/17",
         "VALOR":"10",
         "NOMEBANCO":"",
         "NOMECHEQUE":"",
         "NUMBANCO":"",
         "AGENCIA":"",
         "NUMCONTA":"",
         "NUMCHEQUE":"",
         "DATAATU":"30/12/99",
         "PARCELA":"3 - 6"
       },
       {
         "ID":"106",
         "ID_PEDIDO":"19",
         "ID_EMPRESA":"107868",
         "FORMA":"Boleto",
         "VENCIMENTO":"13/10/17",
         "VALOR":"10",
         "NOMEBANCO":"",
         "NOMECHEQUE":"",
         "NUMBANCO":"",
         "AGENCIA":"",
         "NUMCONTA":"",
         "NUMCHEQUE":"",
         "DATAATU":"30/12/99",
         "PARCELA":"4 - 6"
       },
       {
         "ID":"107",
         "ID_PEDIDO":"19",
         "ID_EMPRESA":"107868",
         "FORMA":"Boleto",
         "VENCIMENTO":"20/10/17",
         "VALOR":"10",
         "NOMEBANCO":"",
         "NOMECHEQUE":"",
         "NUMBANCO":"",
         "AGENCIA":"",
         "NUMCONTA":"",
         "NUMCHEQUE":"",
         "DATAATU":"30/12/99",
         "PARCELA":"5 - 6"
       },
       {
         "ID":"108",
         "ID_PEDIDO":"19",
         "ID_EMPRESA":"107868",
         "FORMA":"Boleto",
         "VENCIMENTO":"27/10/17",
         "VALOR":"10",
         "NOMEBANCO":"",
         "NOMECHEQUE":"",
         "NUMBANCO":"",
         "AGENCIA":"",
         "NUMCONTA":"",
         "NUMCHEQUE":"",
         "DATAATU":"30/12/99",
         "PARCELA":"6 - 6"
       }
       ]
     }
    }
    
asked by anonymous 12.09.2017 / 20:47

1 answer

2

You can use the System.JSON classes

Here is an example of what your JSON looks like to get the ID's (There may be more direct mode but you already have something to start)

procedure ParseJSON(JSONText:String);
var
  js:TJSONObject;
  jDadosPedido:TJSONObject;
  jFormapagto:TJSONObject;
  IDFormapagto:string;
  jArrFormapagto:TJSONArray;
  iter:integer;
begin
  js:=TJSONObject(TJSONObject.ParseJSONValue(JSONText));
  if js<>nil then
  begin
    jDadosPedido:=TJSONObject(js.Values['dadospedido']);
    if jDadosPedido<>nil then
    begin
      jArrFormapagto:=TJSONArray(jDadosPedido.Values['formapagto']);
      for iter := 0 to jArrFormapagto.Count-1 do
      begin
        jFormapagto:=TJSONObject(jArrFormapagto.Items[iter]);
        IDFormapagto:=jFormapagto.Values['ID'].Value;
      end;

    end;
    js:=nil;
  end;
end;
    
13.09.2017 / 00:02