Hybrid 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]);
        }
    });

  3. #2
    Ext JS Premium Member tvanzoelen's Avatar
    Join Date
    Apr 2008
    Location
    Groningen - Netherlands
    Posts
    1,118
    Answers
    85
    Vote Rating
    30
    tvanzoelen has a spectacular aura about tvanzoelen has a spectacular aura about tvanzoelen has a spectacular aura about

      0  

    Default


    Better then to return metadata in the response, and decode the response

    Code:
    var obj = Ext.decode(r.responseText);
    From the MetaData create a new Store and reconfigure the grid

    Code:
    grid.reconfigure(newStore)
    Also here a post about the subject

    http://www.sencha.com/forum/showthre...er-json-object

  4. #3
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,604
    Answers
    543
    Vote Rating
    325
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    Do you want to keep those strings ("cedf-c56f-18a4-4b1", etc.) or do you just want to throw them away and read only the name and state?

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

      0  

    Default


    Quote Originally Posted by skirtle View Post
    Do you want to keep those strings ("cedf-c56f-18a4-4b1", etc.) or do you just want to throw them away and read only the name and state?
    @skirtle

    Thank you for your reply
    I don't think that I'm going to need them as they are embedded in the data, so I could use the embedded one directly

    For example:

    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"
            },
            .
            .
            .
        }
    }
    Sorry, I forgot to mention that

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

      0  

    Default


    Any Suggestions?

  7. #6
    Ext JS Premium Member tvanzoelen's Avatar
    Join Date
    Apr 2008
    Location
    Groningen - Netherlands
    Posts
    1,118
    Answers
    85
    Vote Rating
    30
    tvanzoelen has a spectacular aura about tvanzoelen has a spectacular aura about tvanzoelen has a spectacular aura about

      1  

    Default


    Get the data with a Json Ajax Request and on callback process it

    Code:
    var obj = Ext.decode(response.responseText);
    you can iterate the object properties with code something like

    Code:
      var users = obj.users;
    
      for(var item in users){
        
           var user = users[item];
           var name = user.name;
           
    
       }

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

      0  

    Default


    Quote Originally Posted by tvanzoelen View Post
    Better then to return metadata in the response, and decode the response
    @tvanzoelen

    Thank you for your reply
    But There is no metadata in the response text.
    Also I don't have a grid to display the data in, I will display the some data in my customized panel, and other data in other panel on user request

Thread Participants: 2

Tags for this Thread