1. #1
    Ext User
    Join Date
    Jun 2009
    Posts
    5
    Vote Rating
    0
    jmeyer09 is on a distinguished road

      0  

    Default JSONReader for JSON with multiple root elements

    JSONReader for JSON with multiple root elements


    Hello,

    I am trying to figure out if there is a way to set up a reader to read multiple root elements.

    Currently, my dataStore is setup like this...


    // Define Shapes Datastore

    // Airport data store

    shapeStore =
    new Ext.data.Store({
    url: myUrl,
    reader:
    new Ext.data.JsonReader({
    root:
    'schedshp'

    },[
    'type',
    'airspace_name',
    'start_time',
    'end_time',
    'style',
    'geom'

    ]),
    });

    I have recently changed the JSON to be like this...

    { "schedshp": [], "astype": [ {"astype": "atcaa"}, {"astype": "arial_rr"}, {"astype": "alert"}, {"astype": "moa"}, {"astype": "other"}, {"astype": "prohibited"}, {"astype": "restricted"}, {"astype": "warning"} ], "acttype": [ {"actype": "active"}, {"actype": "_1hour"}, {"actype": "1h_2h"}, {"actype": "2h_4h"}, {"actype": "4h_8h"} ], "alttype": [ {"alttype": "surf_10"}, {"alttype": "10_17"}, {"alttype": "FL180_FL280"} ] }

    In addition to being able to read the "schedshp" root, I'd like to also be able to read the astype, acttype and alttype root elements.

    Any help would be much appreciated.

    Thanks,
    Jim

  2. #2
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,546
    Vote Rating
    64
    Animal is a jewel in the rough Animal is a jewel in the rough Animal is a jewel in the rough

      0  

    Default


    If you are in control of the JSON, why not just create a simple Array of rows container the data for each row?

  3. #3
    Ext User
    Join Date
    Jun 2009
    Posts
    5
    Vote Rating
    0
    jmeyer09 is on a distinguished road

      0  

    Default I think that'd work... but how about something like this

    I think that'd work... but how about something like this


    Hello,

    While you were responding I found this somewhere in the forum...

    ds1.load({
    callback: function(){
    ds2.loadData(ds1.reader.jsonData);
    }
    });

    Would something like this work too?

    Thanks,
    Jim

  4. #4
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    97
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    That code is used to load 2 stores from the same request.

    Is that what you want or did you want to load one store with data from 2 roots.

  5. #5
    Ext User
    Join Date
    Jun 2009
    Posts
    5
    Vote Rating
    0
    jmeyer09 is on a distinguished road

      0  

    Default More info....

    More info....


    Currently, the first datastore makes the server request. The JSON being returned has multiple roots. If the second datastore could reference a different root, then I'd be fine. Is that possible?

    Thanks,
    Jim

  6. #6
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    97
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Yes, simply use the code you found.

  7. #7
    Ext User
    Join Date
    Jun 2009
    Posts
    5
    Vote Rating
    0
    jmeyer09 is on a distinguished road

      0  

    Default Here is what worked - Thanks!

    Here is what worked - Thanks!


    I thought I'd just post what worked for me...

    astypeStore =
    new Ext.data.Store({
    reader:
    new Ext.data.JsonReader({
    root:
    'astype'

    },[
    'astype'

    ])
    });


    // Define Shapes Datastore

    // Airport data store

    shapeStore =
    new Ext.data.Store({
    url: myUrl,
    reader:
    new Ext.data.JsonReader({
    root:
    'schedshp'

    },[
    'type',
    'airspace_name',
    'start_time',
    'end_time',
    'style',
    'geom'

    ])
    });

    shapeStore.on(
    'load', function(ds){


    astypeStore.loadData(shapeStore.reader.jsonData);
    ....
    }

    Using that code, I wound up with:
    1) One server call - the myURL in the shapeStore
    2) Two datastores
    3) Each datastore using its own root element

    Thanks for your support,
    Jim

  8. #8
    Ext JS Premium Member cnelissen's Avatar
    Join Date
    Sep 2007
    Location
    California
    Posts
    225
    Vote Rating
    8
    cnelissen will become famous soon enough

      0  

    Default


    If anyone needs a more succinct example, this is what I used to make this work:

    PHP Code:
    var store1 = new Ext.data.Store({
        
    url'/path/to/json-data.php',
        
    method'POST',
        
    autoLoadtrue,
        
    reader: new Ext.data.JsonReader({
            
    root'root1',
            
    fields: ['field1''field2''field3']
        }),
        
    listeners: {
            
    'load': function() {
                
    store2.loadData(store1.reader.jsonData);
            }
        }
    });

    var 
    store2 = new Ext.data.Store({ 
        
    reader: new Ext.data.JsonReader({
            
    root'root2',
            
    fields: ['field1''field2''field3']
        })
    }); 
    Hope that helps someone out there.
    - Clint Nelissen

  9. #9
    Sencha User
    Join Date
    Oct 2009
    Posts
    18
    Vote Rating
    0
    riuslex is on a distinguished road

      0  

    Default


    Thank you all!

  10. #10
    Sencha User
    Join Date
    Mar 2012
    Posts
    1
    Vote Rating
    0
    wells_chao is on a distinguished road

      0  

    Default


    I can't call the reader.jsonData with extjs 4.0 framework.

    what can i try to replace this method ?

    Thanks.