1. #1
    Sencha User
    Join Date
    Feb 2013
    Posts
    66
    Vote Rating
    0
    vijayakumar84 is on a distinguished road

      0  

    Default Parsing Nested array value in json response problem i

    Parsing Nested array value in json response problem i


    Hi All,
    I am trying to parse childId Value from below response in extjs4.1 .I am newbee for association and hasMany concepts .But I am not able to get the value. can anybody tell whether i have defined model correct ?.Can anybody tell what is problem How to fix it..Is am doing right way ?

    sample.Json
    Code:
    {
            "parents":{
             "parent1":{
               "parent":[
                 {
                     "children":{
                         "child":[
                          {
                              "childId":1,
                              "childdetails": {
                      "childrendetails": [
                        {
                          "childName": "Test",
                          "childAge": "19"
                        },
                        {
                          "childName": "Test1",
                          "childAge": "20"
                        }
                      ]
                    }
                              
                      },
                          {
                              "childId":2,
                               "childdetails": {
                      "childrendetails": [
                        {
                          "childName": "Test2",
                          "childAge": "25"
                        },
                        {
                          "childName": "Test3",
                          "childAge": "24"
                        }
                      ]
                    } 
                              
                              
                          }
                       ]
                    }
                 },
                 {
                "children":{
                       "child":[
                          {
                             "childId":3,
                             "childdetails": {
                      "childrendetails": [
                        {
                          "childName": "Test4",
                          "childAge": "25"
                        },
                        {
                          "childName": "test5",
                          "childAge": "21"
                        }
                      ]
                    }
                      
                                
                          },
                          {
                             "childId":4,
                              "childdetails": {
                      "childrendetails": [
                        {
                          "childName": "Test6",
                          "childAge": "26"
                        },
                        {
                          "childName": "test7",
                          "childAge": "27"
                        }
                      ]
                    }
                         }
                   
                   
                                 
                   
                       ]
                    },
                    
                    
                   "parentName":"firstName",
                   "parentAge":"28"
                                  
                 },
                 
                 
                 {
                     "children":{
                         "child":[
                          {
                              "childId":5,
                              "childdetails": {
                      "childrendetails": [
                        {
                          "childName": "Test8",
                          "childAge": "24"
                        },
                        {
                          "childName": "Test9",
                          "childAges": "27"
                        }
                      ]
                    }
                              
                              
                          },
                          {
                              "childId":6,
                               "childdetails": {
                      "childrendetails": [
                        {
                          "childName": "Test10",
                          "childAge": "25"
                        },
                        {
                          "childName": "Test11",
                          "childAge": "24"
                        }
                      ]
                    } 
                              
                              
                          }
                       ]
                    }
                 },
                 {
                    "children":{
                       "child":[
                          {
                             "childId":7,
                             "childdetails": {
                      "childrendetails": [
                        {
                          "childName": "Test12",
                          "childAge": "25"
                        },
                        {
                          "childName": "Test13",
                          "childAge": "21"
                        }
                      ]
                    }
                      
                                
                          },
                          {
                             "childId":8,
                              "childdetails": {
                      "childrendetails": [
                        {
                          "childName": "Test14",
                          "childAge": "26"
                        },
                        {
                          "childName": "Test15",
                          "childAge": "27"
                        }
                      ]
                    }
                         }
                   
                                               
                    ]
                    },
                    
                    
                   "parentName":"secondname",
                   "parentAge":"35"
                                  
                 }
                 
                ]
              
           }
           
                 
        }
        
        }

    ParentModel.js

    Code:
    Ext.define("extcityview.model.ParentModel", {
        extend: 'Ext.data.Model',
         fields: [
            'parentName', 'parentAge'
        ],
        hasMany: {model: 'extcityview.model.ChildrenModel', name: 'children'},
       
    });
    
    
    
    
    Ext.define("extcityview.model.ChildrenModel", {
        extend: 'Ext.data.Model',
        hasMany  : {model:'extcityview.model.ChildModel', name:'child', associationKey: 'childItems'},
        belongsTo: 'extcityview.model.ParentModel'
    });
    
    
    
    
    Ext.define("extcityview.model.ChildModel", {
        extend: 'Ext.data.Model',
        fields: [
             'childId'
        ],
         belongsTo: 'extcityview.model.ChildrenModel'
    });

    ChildStore.js

    Code:
    Ext.define('extcityview.store.ChildStore', {
    	extend : 'Ext.data.Store',
    	storeId : 'samplestore',
    	model : 'extcityview.model.ParentModel',
    	autoLoad : 'true',
    	proxy : {
    		type : 'ajax',
    		url : 'sample.json',
    		reader : {
    			type  : 'json',
                root  :'parents.parent1.parent'		
    		}
    	}
    
    
    });
    In app.js alert the value


    Code:
    store.load({
    			callback: function() 
    			{
    			  console.log("i am in callback");
    				var parent = store.first();
    				alert("parent"+parent);
    				console.log("parent"+parent);
    				parent.children().each(function(children){
    				 children.child().each(function(child)	{
    				 alert("sub region anme"+child.get('childId'));
    				 console.log("sub region name"+child.get('childId'));
    				 	
    				 });
    			    
    			    
    			    
    			    
    				}); 
    			}
            });
    Thanks

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    34,121
    Vote Rating
    453
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    Something that commonly trips people up is they expect the framework to always know where to find the data meaning what property in your response to use for the data. To specify what property, you should use the associationKey config on the association.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!

    When posting code, please use BBCode's CODE tags.

Tags for this Thread