Andrea Matthiae
asked on
Delphi DataSnap REST problem with TJSONObject
Hi Experts!
I have been using DataSnap REST for some weeks but I have problems understanding some concepts related to TJSONObject and how these data are sent.
I have three related questions.
I consider a simple class:
.....
In my code I have something similar to:
[First question]
When I send this JSON:
{"name":"John","surname":" Ace","myte xt":""}
i don't want to have:
"mytext":""
in the JSON.
[Second question]
I don't know a way to insert a JSON array like this:
{"name":"John","file": [{"name_file":"myfile1.txt ","dir_fil e":"C:\myd ir"},{"nam e_file":"m yfile2.txt ","dir_fil e":"C:\myd ir2"},...] }
[Third question]
I am looking for a method to manipulate the:
VJSONObject.ToString
before insert in the:
RESTRequest1.AddBody(VJSON Object.ToJ SON,...
but I couldn't find it. I search something like:
VJSONObject := Json.StringToJsonObject(VJ SONObject. ToString);
I have a lot of problems because there is no documentation on this part.
Does anyone have a solution?
Working with RAD Studio Tokyo 10.2.3
I have been using DataSnap REST for some weeks but I have problems understanding some concepts related to TJSONObject and how these data are sent.
I have three related questions.
I consider a simple class:
TMyclass = class
private
[JSONName('name')] // invio
Fname: string;
[JSONName('surname')] // invio
Fsurname: string;
[JSONName('mytext')] // invio
Fmytext: string;
procedure Setname(const Value: string);
procedure Setsurname(const Value: string);
procedure Setmytext(const Value: string);
public
property name: string read Fname write Setname;
property surname: string read Fsurname write Setsurname;
property mytext: string read Fmytext write Setmytext;
end;
.....
procedure TMyclass.Setname(const Value: string);
begin
Fname := Value;
end;
procedure TMyclass.Setsurname(const Value: string);
begin
Fsurname := Value;
end;
procedure TMyclass.Setmytext(const Value: string);
begin
Fmytext := Value;
end;
.....
In my code I have something similar to:
Procedure TMain.Send_JSON;
var
VJSONObject: TJSONObject;
VMyclass:TMyclass;
begin
VMyclass:=TMyclass.Create;
VMyclass.name:='John';
VMyclass.surname:='Ace';
VMyclass.mytext:='';
VJSONObject:=TJson.ObjectToJsonObject(VMyclass);
//...
RESTClient1.ResetToDefaults;
RESTRequest1.ResetToDefaults;
RESTResponse1.ResetToDefaults;
RESTClient1.BaseURL:='http://111.222.333.444:8080/datasnap/rest/mypath';
RESTRequest1.Timeout:=RESTRequest_Timeout;
RESTRequest1.Resource:='/MyParam';
RESTRequest1.Method:=TRESTRequestMethod.rmPOST;
RESTRequest1.AddBody(VJSONObject.ToJSON, ContentTypeFromString('application/json'));
//...
RESTRequest1.Execute;
//...
end;
[First question]
When I send this JSON:
{"name":"John","surname":"
i don't want to have:
"mytext":""
in the JSON.
[Second question]
I don't know a way to insert a JSON array like this:
{"name":"John","file": [{"name_file":"myfile1.txt
[Third question]
I am looking for a method to manipulate the:
VJSONObject.ToString
before insert in the:
RESTRequest1.AddBody(VJSON
but I couldn't find it. I search something like:
VJSONObject := Json.StringToJsonObject(VJ
I have a lot of problems because there is no documentation on this part.
Does anyone have a solution?
Working with RAD Studio Tokyo 10.2.3
ASKER
Hi Ryan,
thanks for the response.
[First question]
I can't remove the declaration at runtime in the delphi code.
I rewrite the question:
if I have
{"name":"John","surname":" Ace","myte xt":"abcde "}
or
{"name":"John","surname":" Ace","myte xt":"zxcvd feye"}
and in a particular case I have:
{"name":"John","surname":" Ace","myte xt":""}
with
"mytext":""
how can I remove "mytext":"" to have
{"name":"John","surname":" Ace"} ?
[Second question]
"... you need to create an array for the files and you need to include it in your class definition".
How can I do it? This is exactly my question.
[Third question]
When I stay in debug I see VJSONObject.ToString. I save also this string in my DB before performing the execute
thanks for the response.
[First question]
I can't remove the declaration at runtime in the delphi code.
I rewrite the question:
if I have
{"name":"John","surname":"
or
{"name":"John","surname":"
and in a particular case I have:
{"name":"John","surname":"
with
"mytext":""
how can I remove "mytext":"" to have
{"name":"John","surname":"
[Second question]
"... you need to create an array for the files and you need to include it in your class definition".
How can I do it? This is exactly my question.
[Third question]
When I stay in debug I see VJSONObject.ToString. I save also this string in my DB before performing the execute
[First question]In this case, you probably can save the value of VJSONObject.ToJSON into a variable, and then do the text comparing and replace it with empty string.
I can't remove the declaration at runtime in the delphi code.
with
"mytext":""
how can I remove "mytext":"" to have
{"name":"John","surname":"Ace"} ?
[Second question]sorry that can't really answer you this in Delphi, I'm more like a .NET guy these days.
"... you need to create an array for the files and you need to include it in your class definition".
How can I do it? This is exactly my question.
[Third question]Ok... so you can save the object as text in DB before performing the execution. So, your question is... ?
When I stay in debug I see VJSONObject.ToString. I save also this string in my DB before performing the execute
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
you can try remove:
Open in new window
think you need to create an array for the files and you need to include it in your class definition. So, when you serialize your class to JSON, you will get the file names included.
Not too sure what exactly what you're asking, but VJSONObject.ToString may not be needed or valid. In your IDE, are you able to debug with break points? or have a Watch to view the objects in run time?