Hi Folks
Hi Folks,
Got an issue with a JSON server app. This is a DataSnap REST Project.
Using this code to generate a TJSONArray
function TServerMethods1.ObjectList: TJSONArray;
var
i: Integer;
lMyObject: TMyObject;
begin
Result := TJSONArray.Create;
for i := 1 to 5 do
begin
lMyObject := TMyObject.Create('Title ' + i.ToString,i);
Result.AddElement(TJson.ObjectToJsonObject(lMyObject));
lMyObject.Free;
end;
end;
Where TMyObjcet is
TMyObject = class(TObject)
private
FNumber: Integer;
FTitle: string;
public
constructor Create(aTitle: string; aNumber: Integer);
property Number: Integer read FNumber;
property Title: string read FTitle;
end;
When accessed via a browser I get:
{"result":[{"number":1,"title":"Title 1"},{"number":2,"title":"Title 2"},{"number":3,"title":"Title 3"},{"number":4,"title":"Title 4"},{"number":5,"title":"Title 5"}]}
But accessing it via Delphi and I get
{"result":[[{"number":1,"title":"Title 1"},{"number":2,"title":"Title 2"},{"number":3,"title":"Title 3"},{"number":4,"title":"Title 4"},{"number":5,"title":"Title 5"}]]}
Note the [[ after result meaning I am getting back two TSONArrays.
Why could this be? Using TRestClient components
I have a couple of projects that demonstrate this.
Dave Craggs
Got an issue with a JSON server app. This is a DataSnap REST Project.
Using this code to generate a TJSONArray
function TServerMethods1.ObjectList: TJSONArray;
var
i: Integer;
lMyObject: TMyObject;
begin
Result := TJSONArray.Create;
for i := 1 to 5 do
begin
lMyObject := TMyObject.Create('Title ' + i.ToString,i);
Result.AddElement(TJson.ObjectToJsonObject(lMyObject));
lMyObject.Free;
end;
end;
Where TMyObjcet is
TMyObject = class(TObject)
private
FNumber: Integer;
FTitle: string;
public
constructor Create(aTitle: string; aNumber: Integer);
property Number: Integer read FNumber;
property Title: string read FTitle;
end;
When accessed via a browser I get:
{"result":[{"number":1,"title":"Title 1"},{"number":2,"title":"Title 2"},{"number":3,"title":"Title 3"},{"number":4,"title":"Title 4"},{"number":5,"title":"Title 5"}]}
But accessing it via Delphi and I get
{"result":[[{"number":1,"title":"Title 1"},{"number":2,"title":"Title 2"},{"number":3,"title":"Title 3"},{"number":4,"title":"Title 4"},{"number":5,"title":"Title 5"}]]}
Note the [[ after result meaning I am getting back two TSONArrays.
Why could this be? Using TRestClient components
I have a couple of projects that demonstrate this.
Dave Craggs
Thanks Apostolis - prefer your solution.
ReplyDeleteDavid Craggs
ReplyDeleteI've tried that. It works but it means that you must parse every response you send to the clients, which is quite expensive in my opinion.
I've used the solution I provided in several occasions and it has served me well, especially when you need to feed the JSON to a third party client/plugin which expects standard format.
Try it, it's quite easy to implement.
Thanks Apostolis Tympakianakis I am using your solution. :-)
ReplyDelete