Datasnap RESTful JSON结果关于?

时间:2012-09-20 08:06:50

标签: json rest datasnap

我有一个TDataset JSON帮助函数。

我想结果返回以下内容:

{"result":[{"rid":"2","firstname":"veli","lastname":"deli"},{"rid":"1","firstname":"ismail","lastname":"kocacan"}]}

但是。不行。返回以下内容:

{"result":[[{"rid":"2","firstname":"veli","lastname":"deli"},{"rid":"1","firstname":"ismail","lastname":"kocacan"}]]}

我想删除额外的[字符...

如何提取此数据并返回?

{"rid":"2","firstname":"veli","lastname":"deli"},{"rid":"1","firstname":"ismail","lastname":"kocacan"}

我的TDataset JSON Helper功能:

 TDatasetJSONHelper = class helper for TDataset
  public
    function ToJSONData: TJSONArray;
    function ToJSONDataWrapper: TJSONValue;
  end;

function TDatasetJSONHelper.ToJSONData: TJSONArray;
var
  jso: TJSONObject;
  jsa: TJSONArray;
  jsp: TJsonPair;
  J: Integer;
  avalue: TJSONValue;
begin
  Self.Close;
  Self.Open;
  jsa := TJSONArray.Create();
  while not Self.Eof do
  begin
    jso := TJSONObject.Create();
    for J := 0 to FieldCount - 1 do
      jso.AddPair(TJsonPair.Create(Fields[J].DisplayName, Fields[J].Value));
    jsa.AddElement(jso);
    Self.Next;
  end;
  Self.Close;
  Result := jsa;
end;


function TDatasetJSONHelper.ToJSONDataWrapper: TJSONValue;
var
  aJSON: TJSONObject;
  apair: TJsonPair;
  avalue: TJSONValue;
begin
  aJSON := TJSONObject.ParseJSONValue
    (TEncoding.ASCII.GetBytes
    ('{"result":[[{"rid":"2","firstname":"veli","lastname":"deli"},{"rid":"1","firstname":"ismail","lastname":"kocacan"}]]}'),
    0) as TJSONObject;

  apair := aJSON.Get(0);
  avalue := apair.JsonValue;

  // ??? avalue := '{"rid":"2","firstname":"veli","lastname":"deli"},{"rid":"1","firstname":"ismail","lastname":"kocacan"}';
  Result := avalue;
end;






function TServerMethods1.GetPersonList: TJSONArray;
begin
  Result := dm.AdoQuery1.ToJSONData;
end;

function TServerMethods1.GetPersonList2: TJSONValue;
begin
  Result := dm.AdoQuery1.ToJSONDataWrapper;
end;

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

GetInvocationMetadata()。ResponseCode:= 200;

GetInvocationMetadata()。ResponseContent:='{“rid”:“2”,“firstname”:“veli”,“lastname”:“deli”},{“rid”:“1”,“firstname”: “伊斯梅尔”, “姓”: “kocacan”}“;

答案 1 :(得分:2)

仅供参考,我们在开源存储库中添加了更完整,更快速的功能。

它是我们 mORMot 框架的一部分,但可以作为独立单元使用,与其他功能无关。

SynVirtualDataSet.pas

function DataSetToJSON(Data: TDataSet): RawUTF8 

请参阅this commitassociated forum thread