【发布时间】:2016-04-20 10:38:32
【问题描述】:
我正在尝试旋转使用 Datasnap 返回的数据集的结果。
示例:这就是我得到的
{"result":[{"table":[["REG_KEY",1,0,0,2,3,0,0,false,false,0,false,false],["REG_NAME",1,1,0,128,129,0,0,false,false,0,false,false]],"REG_KEY":["01","02"],"REG_NAME":["BALEARES","CANARIAS"]}]}
我想将其转换为:
[{"REG_KEY":"01","REG_NAME":"BALEARES"},{"REG_KEY":"02","REG_NAME":"CANARIAS"}]
我打算在 DSHTTPWebDispatcher.FormatResult 中对其进行转换,但这是我第一次使用 DataSnap/REST/JSON,我无法理解管理 JSON 值的类。
应该是这样的:
procedure TWebModule1.DSHTTPWebDispatcher1FormatResult(Sender: TObject; var ResultVal: TJSONValue; const Command: TDBXCommand; var Handled: Boolean);
var
Aux: TJSONObject;
NewResult: TJSONObject;
Row: TJSONObject;
NumField, NumRow, MaxFields, MaxRows: integer;
begin
Handled := True;
NewResult := TJSONArray.Create;
Aux := TJSONArray(ResultVal).Get(0); // I get the result as an Object instead of an Array
MaxFields := Aux.Pairs.Count - 1; // I ignore the Pair 0 because it only contains the Dataset structure
MaxRows := TJSONArray(1).Count;
for NumRow := 0 to MaxRows - 1 do begin
Row := TJSONObject.Create;
for NumField := 1 to MaxFields do begin
Row.AddPair(Aux.Pairs[NumField].JsonString,
TJSONArray(Aux.Pairs[NumField].JsonValue).Get(NumRow));
end;
Aux.Add(Row);
end;
Aux.Free;
ResultVal.Free;
ResultVal := NewResult;
end;
但是 TJSONObject 没有 Pairs.Count 方法来知道包含多少对,并且 TJSONArray 没有 Add 方法来直接添加新对象。我很确定这段代码还有很多其他错误。
谁能指导我如何解决它?
【问题讨论】:
标签: json delphi datasnap delphi-10-seattle