Page 1 of 1

REST service with empty Json 2. line

Posted: Thu Apr 19, 2012 5:11 pm
by Ole Henrik Oftedal

Hi!

When I make a new REST service in Datasnap (Delphi) i test it with from the web page from Embarcadero "Server Function Explorer". I get:

Executed: TServerMethods1.GetProjects

Result:

{"result":[{"ProjectID":"1","ProjectName":"Regional sale"},{"ProjectID":"2","ProjectName":"World wide sale"}]}

Perfect. A very simple Json Array with to lines of project info.

Then I make a new Tiggzi App with only one service. When I push the Test button it displays this:

{
"result": [
[
{
"ProjectID": "1",
"ProjectName": "Regional sale"
},
{
"ProjectID": "2",
"ProjectName": "World wide sale"
}
]
]
}

If you study the test result I get an ekstra bracket on line 3. Now Tiggzi is confused and when I populate the respond structure I get

Result
(Empty box)
ProjectID
ProjectName

I need help and I can't find a work around either. The service won't work event if I try to make the parameters manually.

Best regards

Ole Henrik Oftedal
Timeflex Systemer As
http://www.timeflex.com
http://www.timeflex.no


REST service with empty Json 2. line

Posted: Thu Apr 19, 2012 5:55 pm
by maxkatz

Would you be able to share the service URL so we can test it?

You can create the service response from sample JSON response: http://help.gotiggr.com/documentation...


REST service with empty Json 2. line

Posted: Thu Apr 19, 2012 7:13 pm
by Ole Henrik Oftedal

Yes of course. Here it is:

http://timeflexstart.timeflex.no:8082...

(No parameters needed)

The server is set up with Bacic Authentication. But I don't think you need it in the header of the request. I have tried with both. This is a test server and there no checking on user or password.

//Embarcadero Rad Studio Datasnap very simple REST method.
//Returns two projects
//No parameters needed.
function TServerMethods1.GetProjects: TJSONArray;
var
Obj1, Obj2 : TJSonObject;

begin
//Make first array element...
Obj1:=TJsonObject.Create;
Obj1.AddPair('ProjectID', '1');
Obj1.AddPair('ProjectName', 'Regional sale');

//Second array element...
Obj2:=TJsonObject.Create;
Obj2.AddPair('ProjectID', '2');
Obj2.AddPair('ProjectName', 'World wide sale');

//Add them both to the result of the REST server method
Result := TJSONarray.Create;
Result.Add(Obj1);
Result.Add(Obj2);
end;


REST service with empty Json 2. line

Posted: Thu Apr 19, 2012 7:52 pm
by maxkatz

I think the result is the same, when you open the URL in browser and when using Test in Tiggzi:

In browser:
Image

Copied output from Tiggzi test (formatted):
Image


REST service with empty Json 2. line

Posted: Fri Apr 20, 2012 6:04 am
by Ole Henrik Oftedal

Hi Max!

You are right (again). When I used Mozilla Firefox instead of ie i can see tha same as you.

Do you thing its a Delphi bug? Is this legal JSON formatted code at all? I will now try the Delphi community and a bunch of other things.

-oho


REST service with empty Json 2. line

Posted: Fri Apr 20, 2012 8:43 am
by Ole Henrik Oftedal

How to recreate:

  1. Get the latest RAD Studio XE2 update 4 (the latest)

  2. Use wizzard to create New REST server

  3. Add a new server method (An array with values 1, 2, and 3)

    function TServerMethods1.GetProjects2: TJsonArray;
    begin
    Result := TJSonArray.Create;
    Result:=Result.Add(1);
    Result:=Result.Add(2);
    Result:=Result.Add(3);
    end;

    My result is this:

    {"result":[[1,2,3]]}

    Have anyone seen a result like this. Is it an array inside an array?
    And can we use that type of response in Tiggzy? There are over one million Delphi apps out there ande I bet they also want to discoever REST + Tiggzy. We are going to make them rule ... soon ... :-)


REST service with empty Json 2. line

Posted: Fri Apr 20, 2012 4:04 pm
by maxkatz

You can always check if JSON is valid here: http://jsonlint.com/


REST service with empty Json 2. line

Posted: Fri Apr 20, 2012 9:32 pm
by Ole Henrik Oftedal

Problem solved :-)

In stead of creating an array (TJsonArray) first, it ́s better to create a an object ( TJsonobject) first and then put everything inside this one.

Thank you so much for all your help! The above link pointed me in the right direction. Now I can build the use my the mobilegrid automaticly by supplying this service.

Here ́s the Delphi code to produce my test data (Two projects):

function TServerMethods1.GetProjects2: TJsonObject;
var
Obj1, Obj2: TJSONobject;
Arr: TJsonArray;

begin
// Make first project
Obj1 := TJSONobject.create;
Obj1.AddPair('ProjectID', '1');
Obj1.AddPair('ProjectName', 'World wide sale');

// Second project
Obj2 := TJSONobject.create;
Obj2.AddPair('ProjectID', '2');
Obj2.AddPair('ProjectName', 'Latest sale');

//Create a data array containing both projects
Arr:=TJSonArray.Create;
Arr.AddElement(Obj1);
Arr.AddElement(Obj2);

//Add the array and call it "Data"
Result := TJSONobject.create;
Result.Addpair('Data',Arr);

end;