#1 2020-07-01 12:16:28

sihaynes65
Member
Registered: 2020-07-01
Posts: 6

_json not parsing

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

#2 2020-07-01 12:27:36

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: _json not parsing

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

#3 2020-07-01 12:37:29

sihaynes65
Member
Registered: 2020-07-01
Posts: 6

Re: _json not parsing

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

#4 2020-07-01 12:58:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: _json not parsing

Try to read the documentation again and play with the code lines in there.

You seem to be confused about how TDocVariant works.

Offline

#5 2020-07-01 13:12:59

sihaynes65
Member
Registered: 2020-07-01
Posts: 6

Re: _json not parsing

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

#6 2020-07-01 13:37:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: _json not parsing

No, you don't need to convert it into a TDocVariant.
But you need to use late-binding as in the documentation examples.

Offline

#7 2020-07-01 14:35:04

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: _json not parsing

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

#8 2020-07-01 14:52:14

sihaynes65
Member
Registered: 2020-07-01
Posts: 6

Re: _json not parsing

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

#9 2020-07-01 15:04:41

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: _json not parsing

oVariant:= _JsonFast(sResponseBody);

is enough. TDocVariant.New() is not needed at all.

Offline

#10 2020-07-01 15:19:40

sihaynes65
Member
Registered: 2020-07-01
Posts: 6

Re: _json not parsing

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

#11 2020-07-01 16:04:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: _json not parsing

"Late Binding" is explained in the documentation.
https://synopse.info/files/html/Synopse … ml#TITL_80

Offline

#12 2020-07-01 18:27:23

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: _json not parsing

> 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

#13 2020-07-01 19:26:09

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: _json not parsing

Indeed.

And TDocVariantData has the advantage of being more FPC compatible in the long term (FPC has troubles with latebinding).

Offline

#14 2020-07-02 14:42:09

sihaynes65
Member
Registered: 2020-07-01
Posts: 6

Re: _json not parsing

Thanks a lot for your help with this. The TDocVariantData works fine & i can do everything i need to now.Cheers.

Offline

#15 2020-07-02 14:48:43

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: _json not parsing

Great!

big_smile

Offline

Board footer

Powered by FluxBB