You are not logged in.
Pages: 1
Hi, I'm very new to all this & am probably making an obvious mistake but i can't get the _JSON function to work correctly
I'm doing this in delphi 2007:
var oVariant:Variant;
sResponseBody:string
sResponseBody:='{"AgencyId":"ADAM002 ","AgencyName":"Adams agency4 ","SqlUserName":null,"SqlPassWord":null,"Status":"A","Payslips":true,"AWR":true,"Ledger":false,"Timesheets":true,"FromEmail":"","Bespoke":null,"AgencyEmail":"adam@agency.com","ReplyEmail":" ","UserHash":null}'
oVariant:= _JsonFast(fResponseBody);
It doesn't crash but the oVariant just contains the unparsed JSON not the variant representation of it as i'd expect.
Can anyone see what i doing wrong here? Any help would be very much appreciated.
Offline
Why are you saying that it "contains the unparsed JSON"?
I guess you look into the oVariant variable in the debugger or use writeln - and it will be converted as string, i.e. back to JSON.
But it is a TDocVariant. I guess you can write oVariant.AgencyId and get 'ADAM002 ' value...
Offline
Thanks for quick reply. If i look at oVariant in debugger it just looks the same as the input string. If i do oVariant.AgencyID the result it shows is Variant
Offline
Yeh i very new to all this so i am a bit confused. So are you saying i need convert my variant type to a TDocVariant before i can access it?
Offline
This is a feature. From documentation:
https://synopse.info/files/html/Synopse … l#TITLE_39
As a consequence, the Delphi IDE debugger is able to display such variant values as their JSON representation. That is, V1 will be displayed as '{"name":"john","year":1982}' in the IDE debugger Watch List window, or in the Evaluate/Modify (F7) expression tool.
You must access the data as:
ShowMessage(oVariant.AgencyId);
ShowMessage(oVariant.AgencyName);
...
And to change:
oVariant.AgencyId := 'XYZ';
Get string back:
ShowMessage(oVariant); //Cast variant do string
//or use VariantSaveJson(oVariant);
Offline
Hi thanks for the reply. The way you've described it above is how i expected it to work but when i did oVariant.AgencyId is just showed as a value of variant in the debug window not the actual value of the the agencyID.
I've put in this code which i think is what admin was suggesting:
TDocVariant.New(oVariant) ;
oVariant:= _JsonFast(sResponseBody);
& this to seems to work ok now.
Offline
Sorry like i say i'm very new to all this. Don't really understand what you meant by late binding. I've now realised i can access the fields in the above way but they just don't show in the debug window for the reason shown.
Offline
"Late Binding" is explained in the documentation.
https://synopse.info/files/html/Synopse … ml#TITL_80
Offline
> Sorry like i say i'm very new to all this. Don't really understand what you meant by late binding. I've now realised i
> can access the fields in the above way but they just don't show in the debug window for the reason shown.
Delphi debugger is unable to show individual values since it's a complex structure.
You're just starting with mORMot so it may be better to use TDocVariantData instead of TDocVariant wrapper.
var
Temp: TDocVariantData;
sResponseBody: RawUTF8;
begin
sResponseBody := '{"AgencyId":"ADAM002","AgencyName":"Adams agency4"}';
Temp.InitJSON(sResponseBody, JSON_OPTIONS[True]);
memo1.Lines.Add(Temp.S['AgencyName']);
Also take a look at example at: https://synopse.info/forum/viewtopic.php?id=4287
Last edited by igors233 (2020-07-01 18:32:25)
Offline
Thanks a lot for your help with this. The TDocVariantData works fine & i can do everything i need to now.Cheers.
Offline
Pages: 1