How to load TXDataWebDataSet with multi record JSON

I'm using TWebHttpRequest to get a list of staff members from the server.
I'm using a TXDataWebDataSet to hold the fetched data. I am receiving the JSON from my XData server and this is working correctly. The example json is below.

What do I need to do inside the RequestResponse event to load the json into the TXDataWebDataSet? If you could also include any checking for errors so I can make sure I'm using best practices and catching errors etc.

procedure TwfStaff.reqStaffRequestResponse(Sender: TObject; ARequest: TJSXMLHttpRequestRecord; AResponse: string);
begin

   ***** how do I code this to load the TXDataWebDataSet (with error checking)

end;

Cheers,
Paul

The JSON being returned is:

{
    "$id": 1,
    "@xdata.type": "xdcClasses.TStaffList",
    "staff": [
        {
            "$id": 2,
            "@xdata.type": "xdcClasses.TStaffs",
            "STAFFID": 52,
            "STAFF_FIRST": "David",
            "STAFF_SURNAME": "Allison",
            "EMAIL": "dave@westnet.com.au",
            "USER_NAME": "dave@westnet.com.au",
            "IS_ADMIN": false,
            "IS_ACTIVE": true
        },
        {
            "$id": 3,
            "@xdata.type": "xdcClasses.TStaffs",
            "STAFFID": 9,
            "STAFF_FIRST": "Tanya",
            "STAFF_SURNAME": "Batt",
            "EMAIL": "paul@pacsoftware.com.au",
            "USER_NAME": "tanya",
            "IS_ADMIN": true,
            "IS_ACTIVE": true
        },
        {
            "$id": 4,
            "@xdata.type": "xdcClasses.TStaffs",
            "STAFFID": 26,
            "STAFF_FIRST": "Ian",
            "STAFF_SURNAME": "Bradshaw",
            "EMAIL": "ian.bradshaw@tpg.com.au",
            "USER_NAME": "ian.bradshaw",
            "IS_ADMIN": false,
            "IS_ACTIVE": true
        }
    ]
}

Hi Paul,

I believe this was being discussed in the e-mail channel. We can continue from here then.

You could use something like this:

    tblStaffList.SetJsonData(AResponse['staff']);

But if you can't move forward, and if possible, try to provide a very minimal project reproducing the issue.

Thanks Wagner. I have sent a demo project to you via email.

The code you suggested caused an compiler error :

Incompatible types: 'Integer' and 'string'

If I remove the ['staff'] part, it compiles correctly and runs, but then at runtime, I get the error

"Operation cannot be performed on an inactive dataset" if the TXDataWebDataSet is closed when I do the SetJsonData,

and I get "Operation cannot be performed on an active dataset" if I open the TXDataWebDataSet before using SetJsonData.

Cheers,
Paul

The project is rather too big for a support question. Even then I need exact steps to reproduce the issue, I just got presented to the app with lots of options.

In any case, you shouldn't call First without opening the dataset first:


    tblStaffList.Close;
    tblStaffList.SetJsonData(JS.toArray(JS.toObject(TJSJson.parse(AResponse))['staff']));
    tblStaffList.Open; // add this
    tblStaffList.First;