Ver Mensaje Individual
  #1  
Antiguo Hace 3 Semanas
jmbarrio jmbarrio is offline
Miembro
 
Registrado: may 2003
Posts: 72
Reputación: 22
jmbarrio Va por buen camino
Problemas al escribir JSON

Buenas tardes, tengo el siguiente código para montar un archivo Json

Código Delphi [-]
procedure TF_Inicio.btFacturaRapidaClick(Sender: TObject);
var
  MainObject, lJNestedObject, lJRestoredObject, ObjArticu, ObjLinea: TJSONObject;
  Array_Articu,Array_Linea: TJSONArray;

begin

  try
    // Veo las facturas que están pendientes de enviar
    sqlstr := 'Select * from facemi where fae_entregado = ' + '''' + 'N' + '''';
      with F_Data.FDQry do
        begin
          Sql.Clear;
          Active := False;
          Sql.Add(sqlstr);
          Active := True;
          Open;
          Memo1.Visible := True;
          Memo1.Lines.Clear;

          MainObject := TJSONObject.Create;
          Array_Articu := TJSONArray.Create;
          MainObject.AddPair(TJSONPair.Create('facemi', Array_Articu));

          First;
          while not eof do
            begin
              ObjArticu := TJSONObject.Create;
              ObjArticu.AddPair(TJSONPair.Create('fae_serie',FieldByName('FAE_SERIE').AsString));
              ObjArticu.AddPair(TJSONPair.Create('fae_nro',FieldByName('FAE_NRO').AsInteger));
              Array_Articu.Add(ObjArticu);
              sqlstr := 'Select * from lifacl where lfa_serie = ' + '''' + FieldByName('FAE_SERIE').AsString + '''' + ' and lfa_fnro = ' +
                        FieldByName('FAE_NRO').AsInteger.ToString + ' order by lfa_linea asc';
              with F_Data.FDQry1 do
                begin
                  Sql.Clear;
                  Active := False;
                  Sql.Add(sqlstr);
                  Active := True;
                  Open;
                  First;
                  Array_Linea := TJSONArray.Create;
                  while not eof  do
                    begin
                      ObjLinea := TJSONObject.Create;
                      ObjLinea.AddPair(TJSONPair.Create('lfa_serie',FieldByName('LFA_SERIE').AsString));
                      ObjLinea.AddPair(TJSONPair.Create('lfa_fnro',FieldByName('LFA_FNRO').AsInteger));
                      ObjLinea.AddPair(TJSONPair.Create('lfa_linea',FieldByName('LFA_LINEA').AsInteger));
                      Array_Linea.Add(ObjLinea);
                      Next;
                    end;
                  ObjArticu.AddPair(TJSONPair.Create('lifacl', Array_Linea ) ) ;
                end;
              Next;
            end;

          Memo1.Text := MainObject.ToJSON;
          Memo1.Text := copy(Memo1.Text,11, length(Memo1.Text)-11);

          {$IF DEFINED (WIN32) or DEFINED (WIN64)}
            TFile.WriteAllText('jsonfile.json', '[' + MainObject.Format() + ']'); //Solo para windows
          {$ENDIF}

          RestClient3.BaseURL := servidorWS + 'Facemi/Importar';
          RestRequest3.ClearBody;  //Si no se pone solo se puede ejecutar el post una sola vez
          RestRequest3.Body.Add(MainObject.Format(),TRestContentType.ctAPPLICATION_JSON);
          //RestRequest3.Body.Add(Memo1.Text,TRestContentType.ctAPPLICATION_JSON);
          RestRequest3.Execute;

          MainObject.Free;
        end;
  except
    on e: exception do
      showmessage(e.Message);
  end;
end;

El cual me genera el archivo json

Código:
[{
    "facemi": [
        {
            "fae_serie": "AA",
            "fae_nro": 28,
            "lifacl": [
                {
                    "lfa_serie": "AA",
                    "lfa_fnro": 28,
                    "lfa_linea": 1
                },
                {
                    "lfa_serie": "AA",
                    "lfa_fnro": 28,
                    "lfa_linea": 2
                },
                {
                    "lfa_serie": "AA",
                    "lfa_fnro": 28,
                    "lfa_linea": 3
                }
            ]
        },
        {
            "fae_serie": "AA",
            "fae_nro": 29,
            "lifacl": [
                {
                    "lfa_serie": "AA",
                    "lfa_fnro": 29,
                    "lfa_linea": 1
                },
                {
                    "lfa_serie": "AA",
                    "lfa_fnro": 29,
                    "lfa_linea": 2
                }
            ]
        }
    ]
}]
El problema que tengo es que el endpoint no me coge esa esctructura, necesito que el Json tenga la siguiente estructura, pero no se como hacerlo no doy con la tecla.

Código:
[
        {
            "fae_serie": "AA",
            "fae_nro": 28,
            "lifacl": [
                {
                    "lfa_serie": "AA",
                    "lfa_fnro": 28,
                    "lfa_linea": 1
                },
                {
                    "lfa_serie": "AA",
                    "lfa_fnro": 28,
                    "lfa_linea": 2
                },
                {
                    "lfa_serie": "AA",
                    "lfa_fnro": 28,
                    "lfa_linea": 3
                }
            ]
        },
        {
            "fae_serie": "AA",
            "fae_nro": 29,
            "lifacl": [
                {
                    "lfa_serie": "AA",
                    "lfa_fnro": 29,
                    "lfa_linea": 1
                },
                {
                    "lfa_serie": "AA",
                    "lfa_fnro": 29,
                    "lfa_linea": 2
                }
            ]
        }
]
Muchas gracias de antemano, un saludo.
Responder Con Cita