Mega Search
23.2 Million


Sign Up

Make a donation  
DataSet to JSON output for a Java application  
News Group: embarcadero.public.delphi.database.multi-tier

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

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 10-Apr-2014, at 3:24 AM EST
From: lior ilan
 
Re: DataSet to JSON output for a Java application  
News Group: embarcadero.public.delphi.database.multi-tier
 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.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Apr-2014, at 5:44 AM EST
From: lior ilan
 
Re: DataSet to JSON output for a Java application  
News Group: embarcadero.public.delphi.database.multi-tier
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}

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Apr-2014, at 7:41 AM EST
From: Robert Triest
 
Re: DataSet to JSON output for a Java application  
News Group: embarcadero.public.delphi.database.multi-tier
> 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)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Apr-2014, at 8:53 AM EST
From: Robert Triest