1

我有一个 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 助手函数:

 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;

我怎么解决这个问题 ?

4

2 回答 2

2

GetInvocationMetadata().ResponseCode := 200;

GetInvocationMetadata().ResponseContent := '{"rid":"2","firstname":"veli","lastname":"deli"},{"rid":"1","firstname":"ismail" ,"姓氏":"kocacan"}';

于 2014-01-05T11:26:47.423 回答
2

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

它是我们的mORMot框架的一部分,但可以用作独立单元,不与其他功能绑定。

参见SynVirtualDataSet.pas

function DataSetToJSON(Data: TDataSet): RawUTF8 

请参阅此提交相关的论坛主题

于 2014-05-18T10:52:23.767 回答