Delphi XE7 Datasnap with pure JSON

I have this:

function TWS.listJSON(const id: integer): TJSONObject;
var LDataSets: TFDJSONDataSets;
begin
    LDataSets := the_list(id); //the_list:TFDJSONDataSets
    try
       Result := TJSONObject.Create;
       TFDJSONInterceptor.DataSetsToJSONObject(LDataSets, Result);
    finally
       LDataSets.Free;
   end;
end;

All fine, "the_list()" will get all the needed data from my select and finally I will have the result. Some Java clients will connect accessing something like: http://localhost:8080/datasnap/rest/Tws/listJSON/123

To make a try, I installed a Chrome Extension called Advanced Rest Client, and I get such result:

{"list":"QURCUw4AAADGAQAA/wABAAH/Av8DBAAO...."} 

after make some changes in TWebModule1.DSHTTPWebDispatcher1FormatResult();

It seems to be a compressed JSON data and as far as I know, Java can handle that, but I am not sure and I would prefer a uncompressed and pure JSON output. I know that using mORMOt can do the trick but I would like to give a try, as to use mORMOt we should learn a lot.

Is it possible to do that, output pure JSON, by using RAD Datasnap server? Maybe everything is absolutely correct and I am just don't know...


I don't think it will work , I assume you need Delphi units to use FireDacJsonreflect.

You could make the Json output yourself , see this small example I use comany from example DB only 3 field in a clientdataset , you will probobly make it a little bit more complicated, and also another structur I guess

Just an idea..

function TServerMethods1.JsonDB: TJSONObject;  // Hold the array
var
i      : Integer;
JsonArray: TJSONArray;
record_number : Integer;

begin
 result:=TJSONObject.Create;

 // Field names
 JsonArray:=TJSONArray.Create;
 ClientDataSet1.First;
 for i := 0 to ClientDataSet1.Fields.Count-1 do
 Begin
  JsonArray.AddElement(TJSONObject.Create(TJSONPair.Create('Field'+I.ToString    ,ClientDataSet1.Fields[i].FieldName)));
 End;
  Result.AddPair('Fields',JsonArray);

//Data
 record_number:=0;
 while not ClientDataSet1.Eof  do
 Begin
   inc(record_number);
   JsonArray:=TJSONArray.Create;
   for i := 0 to ClientDataSet1.Fields.Count-1 do
   Begin
     JsonArray.AddElement(TJSONObject.Create(TJSONPair.Create(I.ToString,ClientD    ataSet1.Fields[i].Asstring)));
   End;
   Result.AddPair('record-'+record_number.ToString,JsonArray);
   ClientDataSet1.Next;
 End;

end;

This should give a result like

{"result":[{"Fields":[{"Field0":"CustNo"},{"Field1":"Company"},{"Field2":"Country"}],"record-1":[{"0":"1221"},{"1":"Kauai Dive Shoppe"},{"2":"US"}],"record-2":[{"0":"1231"},{"1":"Unisco"},{"2":"Bahamas"}],"record-3":[{"0":"1351"},{"1":"Sight Diver"},{"2":"Cyprus"}],"record-4":[{"0":"1354"},{"1":"Cayman Divers World Unlimited"},{"2":"British West Indies"}],"record-5":[{"0":"1356"},{"1":"Tom Sawyer Diving Centre"},{"2":"US Virgin Islands"}],"record-6":[{"0":"1380"},{"1":"Blue Jack Aqua Center"},{"2":"US"}],"record-7":[{"0":"1384"},{"1":"VIP Divers Club"},{"2":"US Virgin Islands"}],"record-8":[{"0":"1510"},{"1":"Ocean Paradise"},{"2":"US"}],"record-9":[{"0":"1513"},{"1":"Fantastique Aquatica"},{"2":"Columbia"}],"record-10":[{"0":"1551"},{"1":"Marmot Divers Club"},{"2":"Canada"}],"record-11":[{"0":"1560"},{"1":"The Depth Charge"},{"2":"US"}],"record-12":[{"0":"1563"},{"1":"Blue Sports"},{"2":"US"}],"record-13":[{"0":"1624"},{"1":"Makai SCUBA Club"},{"2":"US"}],"record-14":[{"0":"1645"},{"1 ":"Action Club"},{"2":"US"}],"record-15":[{"0":"1651"},{"1":"Jamaica SCUBA Centre"},{"2":"West Indies"}],"record-16":[{"0":"1680"},{"1":"Island Finders"},{"2":"US"}],"record-17":[{"0":"1984"},{"1":"Adventure Undersea"}


procedure TWebModuleServer.DSHTTPWebDispatcher1FormatResult(Sender: TObject;
  var ResultVal: TJSONValue; const Command: TDBXCommand; var Handled: Boolean);
var
JSONValue: TJSONValue;
begin
  if Command.Text = 'TServerMethodsServer.GetVendas' then
    begin
      Handled := True;
      JSONValue := ResultVal;
      ResultVal := TJSONArray(JSONValue).Get(0);
      TJSONArray(JSONValue).Remove(0);
      JSONValue.Free;
    end;
end;
链接地址: http://www.djcxy.com/p/64686.html

上一篇: 如何通过代理服务器将DataSnap客户端连接到DataSnap服务器?

下一篇: 带纯JSON的Delphi XE7 Datasnap