0
votes

What is the root property value if I get a Json like that:

{
    "status": {
        "status": 0,
        "msg": "Ok",
        "protocolversion": "extjs.json"
    },
    "value": {
        "table": [
            [
                "admin",
                "Administrator",
                ""
            ],
            [
                "test",
                "Test",
                ""
            ]
        ],
        "total": 2
    }
}

The data will be displayed in a gridpanel, 1 row is admin, 1 row is test, etc.

Tried:

value, value.table

How to get this to work?

2

2 Answers

1
votes

value.table is correct for the root property, but you are using a json format that I don't think Ext is set up to handle by default. It has a reader for json that is used for an array of objects, not for an nested arrays of field values with no object mapping information.

If you have to use that format, you will either need to create your own readers/writers or just use Ext.Ajax.request(), and in the callback, parse the nested array into objects. Something like:

Ext.Ajax.request({
    url: 'path.com/to/content',
    success: function (response, operation) {
        var data = Ext.JSON.decode(response.responseText);
        var fields = data.value.table;
        var records = [];
        Ext.Array.each(fields, function (fieldArray, fieldIndex) {
            Ext.Array.each(fieldArray, function(fieldValue, valueIndex) {
                //Create record object if it doesn't exist
                var record = records[valueIndex] || {};
                //Create a switch statement based on the array index to set fields
                switch(fieldIndex) {
                    case 0:
                        record.User_id = fieldValue;
                        break;
                    case 1:
                        record.Username = fieldValue;
                        break;
                }
            });
        });

        //Add the objects to the empty store
        store.add(records);
    }
});

That's not a production solution by any means since it doesn't handle that empty string at the end of your list or the case that you get a jagged array of arrays per field which I can't imagine what to do with. If it's within your control or influence, I would suggest using a format more like what Ext suggests so you can use the built in json proxy/reader/writer or go crazy with it and implement ext's remote procedure call format:

{
    "success": true,
    "message": "Success",
    "data": [
        {
            "User_id": "admin",
            "Username": "Administrator"
        }, {
            "User_id": "admin",
            "Username": "Administrator"
        }
    ]
}
0
votes

In above example "value" is root property. But for JSON reader it's a property name (or a dot-separated list of property names if the root is nested).

so you can assign into your field you need following.

fields:['table.admin','table.test']