TWebStringGrid LoadFromJSON in mem, non URL

Hi,

Just as part of my experimentation, I had some JSON pulled back from a XData server and had it in a JSON TJSObject.  I wanted to display it in a grid, so I thought i would LoadFromJSON, but of course that takes a URL, not a JSON object. 

I think it would be good to abstract this into 2 methods, one that takes a URL and the other that takes a TJSObject.  I borrowed the code from TCustomStringGrid.DoHttpLoadJson, but it seemed that you might often want to push a JSON Array (n rows) or even a JSON object (1 row per attribute) into a grid.  Does this seem like a good idea?


Aside:  it had a number of protected methods:
GetCellData(vGrid.FixedCols + c, vGrid.FixedRows + r, nil, cd);
GetCellChildren(vGrid.FixedCols + c, vGrid.FixedRows + r, nil, cd, ce);
GetCellClassName(vGrid.FixedCols + c, vGrid.FixedRows + r, nil, cd, cn);
I'm not 100% sure of the point of them, but I didn't want to subclass the grid for what I wanted.

And one very intriguing one:
asm

        var x = undefined;

        for (x in JO) {

          arow += "|" + JO[x];

          c++;

        }

      end;

What are they for?

We have added the overload LoadFromJSON(AJSON: TJSObject; ADataNode: string) to be able to work with JSON objects directly.
The GetCellXXX methods are protected methods calling event handlers. Their sole purpose is call the possible assigned event handlers.
The ASM code will convert a JSONObject into a delimited string that can then be parsed by a TStringList class.

@brunofierens ~

I have the same issue and I have just too little experience to know how to do this. I can load the JSON from a URL, but the URL now requires a bearer token in the header. Without the bearer token I can load the data fine from the XData server.

So, now with the requirement of a bearer token, I either need the ability to modify the headers for LoadFromJSON or I need the overloaded method you describe here in this post; LoadFromJSON(AJSON: TJSObject; ADataNode: string)

When I try and parse the JSON to a TJSObject and use the method, like this:

procedure(AResponse: string; AReq: TJSXMLHttpRequest)
      var
        JS: TJSON;
        xx: TJSONObject;
      begin
        JS := TJSON.Create;
        try
           xx:=TJSONObject(JS.Parse(AResponse));
           DogDataGrid.LoadFromJSON(xx,'value');
...

I get a error:

Pas2JS Compiler version 2.1.1 [2021/09/19] for Win32 i386 / TMS WEB Core version v1.9.0.0
[Error] MainUnit.pas(222): Incompatible type arg no. 1: Got "TJSONObject", expected "String"

How do I use LoadFromJSON(AJSON: TJSObject; ADataNode: string) ?

Regards
ViV

First parameter is either TJSObject or string , not TJSONObject.
Use therefore,

uses
  js;
var
  xx: TJSObject;
begin
  xx:=TJSObject(TJSJSON.Parse(AResponse));
  DogDataGrid.LoadFromJSON(xx,'value');
end;

@brunofierens ~

Thank you for the reply ...

I see the difference between TJSObject and TJSONObject, I changed it, but it still does not work.

Error:

Pas2JS Compiler version 2.1.1 [2021/09/19] for Win32 i386 / TMS WEB Core version v1.9.0.0
[Error] MainUnit.pas(222): Incompatible type arg no. 1: Got "TJSObject", expected "String"
Error during compilation

Any thoughts on what I am doing wrong?

Regards
ViV

I cannot see why you still get this error. In WEBLib.Grids.pas , there is clearly an overload:

procedure LoadFromJSON(const AURL: string; ADataNode: string = ''); overload;
procedure LoadFromJSON(AJSON: TJSObject; ADataNode: string = ''); overload;

so, the compiler should see the 2nd option.
Can you check that WEBLib.Grids.pas you compile against effectively has this overload in TCustomStringGrid?

@brunofierens ~

I think I found the issue ..

Sorry I did not pick this up in the beginning;

Here is my declaration:

DogDataGrid: TWebResponsiveGrid;

Looking in the WEBLib.Grids, I do not see the the overloaded method :

{$IFNDEF LCLTMSWEB}
[ComponentPlatforms(TMSWebPlatform)]
{$ENDIF}
TWebResponsiveGrid = class(TWebCustomControl)
private
....
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure LoadFromJSON(const AURL: string; ADataNode: string = '');
procedure LoadFromCSV(const AURL: string; Delimiter: char = ';');
....

Is there a way to get the overloaded method into this type of Grid?

Regards
ViV

TWebResponsiveGrid is not having this overload yet. We would need to add it to this class.

@brunofierens ~

Thank you very much. Is this something that will be added or should I find another way around this issue?

Regards
ViV

We have added these overloads and these will be available in the next update.

1 Like