F
You can use Delphi standard support for JSONwhich is in the units http://docwiki.embarcadero.com/Libraries/Rio/en/System.JSON .The basics are parsear the string you get in a type object http://docwiki.embarcadero.com/Libraries/Rio/en/System.JSON.TJSONValue from which you can already extract the values using the method GetValue.I show you first a simple example (not yet responsible for the arrangement):uses
System.JSON;
procedure TForm3.Button1Click(Sender: TObject);
var
sJSON: string;
Datos: TJSONValue;
Tabla: string;
Opcion: Integer;
begin
{ la cadena con el JSON de tu pregunta }
sJSON := '{"tabla":"programacxp","opcion":"1","data":[{"fechapago":"2020-07-17"'
+ ',"semana":29,"concepto":"RECOLECCION DE BASURA ","proyecto":"7E313-C2732"'
+ ',"importe":"100.00","proved":"DDF","status":true,"clasif":"GASTO DE ADMON"'
+ ',"periodo":"0","dias":"5","aplicado":false}]}';
{ conversión de la cadena en un TJSONValue }
Datos := TJSonObject.ParseJSONValue(sJSON);
{ extracción de datos en variables de distintos tipos }
Tabla := Datos.GetValue<string>('tabla');
Opcion := Datos.GetValue<Integer>('opcion');
{ Mostrar datos al usuario }
ShowMessage(Format(''
+ 'Tabla: %s'#13
+ 'Opcion: %d'
, [Tabla, Opcion]));
end;
The arrangement we can handle in various ways, one is using the type http://docwiki.embarcadero.com/Libraries/Rio/en/System.JSON.TJSONArray , allowing us to iterate by the arrangement and treat each of its elements as a TJSONValue.Basically something like:var
...
DatosData: TJSONArray;
DataElemento: TJSONValue;
begin
...
{ obtenemos el arreglo 'data' en el objeto DatosData, de tipo TJSONArray }
DatosData := Datos.GetValue<TJSONArray>('data');
for DataElemento in DatosData do
begin
{ acá puedes utilizar DataElemento.GetValue para extraer los datos de cada elemento }
end;
By putting everything together, we could add the data of all the elements of the arrangement to the message shown to the user of the first example as follows:procedure TForm3.Button1Click(Sender: TObject);
var
sJSON: string;
Datos: TJSONValue;
Tabla: string;
Opcion: Integer;
DatosData: TJSONArray;
DataElemento: TJSONValue;
sData: string;
Contador: Integer;
begin
sJSON := '{"tabla":"programacxp","opcion":"1","data":[{"fechapago":"2020-07-17"'
+ ',"semana":29,"concepto":"RECOLECCION DE BASURA ","proyecto":"7E313-C2732"'
+ ',"importe":"100.00","proved":"DDF","status":true,"clasif":"GASTO DE ADMON"'
+ ',"periodo":"0","dias":"5","aplicado":false}]}';
Datos := TJSonObject.ParseJSONValue(sJSON);
Tabla := Datos.GetValue<string>('tabla');
Opcion := Datos.GetValue<Integer>('opcion');
DatosData := Datos.GetValue<TJSONArray>('data');
sData := '';
Contador := 0;
for DataElemento in DatosData do
begin
sData := sData + Format(''
+ ' Elemento # %d '#13
+ ' FechaPago: %s'#13
+ ' Semana: %d'#13
+ ' Proyecto: %s'#13
, [ Contador
, DateToStr(DataElemento.GetValue<TDate>('fechapago'))
, DataElemento.GetValue<Integer>('semana')
, DataElemento.GetValue<string>('proyecto')
]);
Inc(Contador);
end;
ShowMessage(Format(''
+ 'Tabla: %s'#13
+ 'Opcion: %d'#13
+ 'Data:'#13
+ '%s'
, [Tabla, Opcion, sData]));
end;
Another way to get the data is to indicate the complete route in the method GetData on the TJSONValue containing the entire object, for example:var
...
FechaPago: TDate;
begin
...
FechaPago := Datos.GetValue<TDate>('data[0].fechapago');
ShowMessage(DateToStr(FechaPago));
end;
Finally mention that there are other libraries that offer support to process JSONI personally try to use the RTL, as the code is more standard and has fewer dependencies, but in the end there may be some good reason to use a third-party library and we must not close to the idea.