Threaded View

  1. #1
    Sencha User
    Join Date
    Nov 2012
    Posts
    12
    Vote Rating
    0
    fujy is on a distinguished road

      0  

    Default Answered: Random Field names in JSON Data

    Answered: Random Field names in JSON Data


    Hello,

    I have a problem with JSON response data that I don't how to model it
    Here's an example

    If the JSON data is something like this:

    Code:
    {
        "users" : [
            {
                "name"  : "tom",
                "state" : "WA",
                "id"    : "cedf-c56f-18a4-4b1"
            },
            {
                "name"  : "steve",
                "state" : "NY",
                "id"    : "ebb2-92bf-3062-7774"
            },
            {
                "name"  : "mike",
                "state" : "MA",
                "id"    : "fb60-b34f-6dc8-aaf7"
            },
            .
            .
            .
        ]
    }
    I know that the model of that data will be something like this

    Code:
    Ext.define('Foo.model.User',{
        extend: 'Ext.data.Model',
        
        fields: [
            {name: 'name' ,type: 'string'},
            {name: 'state'  ,type: 'string'},
            {name: 'id'      ,type: 'string'},
        ],    
    });
    But what if the JSON response data are like these
    Note: I don't have any control on the response

    Code:
    {
        "users" : {
            "cedf-c56f-18a4-4b1" : {
                "name"  : "tom",
                "state" : "WA",
                "id"    : "cedf-c56f-18a4-4b1"
            },
            "ebb2-92bf-3062-7774" : {
                "name"  : "steve",
                "state" : "NY",
                "id"    : "ebb2-92bf-3062-7774"
            },
            "fb60-b34f-6dc8-aaf7" : {
                "name"  : "mike",
                "state" : "MA",
                "id"    : "fb60-b34f-6dc8-aaf7"
            },
            .
            .
            .
        }
    }
    And of course the "cedf-c56f-18a4-4b1", "ebb2-92bf-3062-7774" ... etc, are totally unknown to me

    How can I make the Model, Reader, ... etc for this data?

    Thanks in advance

    Edit: The field names that cause the problem (e.g. "cedf-c56f-18a4-4b1") are embedded in the data

  2. You should be able to read that data format fine if you write a subclass of JSON reader. Take a look at getResponseData here:

    http://docs.sencha.com/ext-js/4-1/so...ta-reader-Json

    Overriding readRecords should do it. Just loop through the object and rearrange the structure to something a little more straightforward. It'd be something a bit like this:

    Code:
    Ext.define('MyReader', {
        alias: 'reader.my-reader',
        extend: 'Ext.data.reader.Json',
    
        readRecords: function(data) {
            var oldUsers = data.users,
                newUsers = [],
                key;
    
            for (key in oldUsers) {
                newUsers.push(oldUsers[key]);
            }
    
            data.users = newUsers;
    
            this.callParent([data]);
        }
    });

Thread Participants: 2

Tags for this Thread