将TRestResponse内容转换为TFDJSONDataSets

时间:2015-05-15 14:55:18

标签: rest delphi rest-client

我正在使用delphi XE7,我需要帮助在Rest Client应用程序的客户端将TRestResponse内容转换为TFDJSONDataSets

在下面发布的代码中,我需要一行函数:

LDataSets := function(sContent)-->transform sContent to TFDJSONDataSets

我该怎么做?

//client side
procedure TaskComplete;
var sContent:string;
LDataSets: TFDJSONDataSets;
begin
  sContent:=ModuloCliente.RESTResponse1.content;
  LDataSets := function(sContent)-->Here I need to transform sContent to TFDJSONDataSets
  //Prepare temp MemoryTable 
  memTable.Active := False;
  memTable.AppendData(TFDJSONDataSetsReader.GetListValue(LDataSets, 0));
end;

//request to rest server
procedure DoRequest;
begin
  ModuloCliente.RESTClient1.BaseURL:='http://localhost:8080/datasnap/rest/TsrvServerMetodos';
  ModuloCliente.RESTRequest1.Resource:='"pending_msg_to_user_token"/{param1}';
  ModuloCliente.RESTRequest1.Params.ParameterByName('param1').Value:='1';
  ModuloCliente.RESTRequest1.ExecuteAsync(TaskComplete,true,true);
end;

//server side
function TSrvServerMetodos.pending_msg_to_user_token(qry:string): TFDJSONDataSets;
begin
  qryStaff.Active := False;
  with  qryStaff do begin
    Params.Clear;
    SQL.Clear;
    unprepare;
    SQL.Add(qry);
  end;
  Result := TFDJSONDataSets.Create;
  TFDJSONDataSetsWriter.ListAdd(Result, qryStaff);
end;

1 个答案:

答案 0 :(得分:0)

procedure TaskComplete;
var 
  sContent:string;
  LDataSets: TFDJSONDataSets;
  oJsonObject: TJSONObject;
begin
  oJsonObject: ModuloCliente.RESTResponse1.Response.JSONValue as TJSONObject;

  LDataSets:= TFDJSONDataSets.Create;
  TFDJSONInterceptor.JSONObjectToDataSets(oJsonObject, LDataSets);

  //Prepare temp MemoryTable 
  memTable.Active := False;
  memTable.AppendData(TFDJSONDataSetsReader.GetListValue(LDataSets, 0));
end;