Hi,
I need to return a JSON representation of a dataset from my Delphi
application to a java android application on its request.
Is using a DataSnap server application the right solution?
How do I convert the dataset to the JSON format?
Thanks in advance
wrote in message
> ...
> See if datasnap has now direct JSON support automatic, if not
> then DBXJSON will do it
> ...
>
Thank you for the example. I didn't find automatic conversion in XE5
DataSnap.
Yes you could use a datasnap server application to manage you requests.
It's like CGI programming; declare your functions in Delphi and request them
on the client side (like javascript) but.. without installing a complete webserver.
(IIS, Apache)
In the functions that returns data you can use the db components to access
your database and use the DBXJSON library to create a JSON string and
return this to the client.
See if datasnap has now direct JSON support automatic, if not
then DBXJSON will do it:
{code}
Function ReturnJSONString: String;
var
jsobj, jso : TJsonObject;
jsa : TJsonArray;
jsp : TJsonPair;
Begin
jsObj := TJsonObject.Create();
jsa := TJsonArray.Create();
jsp := TJSONPair.Create('Array', jsa);
jsObj.AddPair(jsp);
//code that loops through your database resultset
while not eof do
begin
jso := TJsonObject.Create();
strDate:=MyField.AsDateTime;
jso.AddPair(TJsonPair.Create('Date',strDate));
Amount:=MyField.AsFloat;
jso.AddPair(TJsonPair.Create('Value',FormatFloat('0.00',Amount));
jsa.AddElement(jso);
MyQuery.Next;
End;
Result:=jsObj.ToString;
End;
{code}
> Thank you for the example. I didn't find automatic conversion in XE5 DataSnap.
No problem, I use JSON like the example and that works very well. You also
have to look how the JSON string should look like on the receiver side. You can
declare the return string in many different ways. (like in this example an Array)