Delphi XE7 Datasnap with pure JSON

I have it:

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; 

Everything is fine, "the_list ()" will get all the necessary data from my selection and, finally, I will get the result. Some Java clients will connect to something like: http: // localhost: 8080 / datasnap / rest / Tws / listJSON / 123

To try, I installed the Chrome extension called Advanced Rest Client, and we get the following result:

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

after making some changes to TWebModule1.DSHTTPWebDispatcher1FormatResult ();

This seems to be compressed JSON data, and as far as I know, Java can handle this, but I'm not sure, and I would prefer uncompressed and clean JSON output. I know that using mORMOt can do the trick, but I would like to try, because we have to learn a lot to use mORMOt.

Is it possible to do this, output pure JSON using the RAD Datasnap server? Maybe everything is absolutely correct, and I just donโ€™t know ...

+5
source share
2 answers

I donโ€™t think this will work, I suppose you need Delphi units to use FireDacJsonreflect.

You can infer Json yourself, see this little example. I use comany from the DB example only 3 fields in clientdataset, you will probably make it a little more complicated, and also another constructor, I think

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, for example

{"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 Center"}, {"2": "US Virgin Islands}}, record-6 ": [{" 0 ":" 1380 "}, {" 1 ":" The center of the blue jack aqua "}, {" 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 Diving Club"}, {"2": "Canada"}], "record-11 ": [{" 0 ":" 1560 "}, {" 1 ":" Depth Charge "}, {" 2 ":" US "}]," record-12 ": [{" 0 ":" 1563 " }, {"1": "Blue Sport"}, {"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's SCUBA Center"}, {"2": "West Indies"}], "record-16" : [{"0": "1680"}, {"1": "Island Finders"}, {"2": "US"}], "record-17": [{"0": "1984"} , {"1": "Underwater Adventure"}

+2
source
 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; 
0
source

All Articles