2013-03-07 119 views
0

我想解析下面的Response中的childId值。我無法解析價值。我是該協會的新手,有許多概念。我在app.js中給出了一個alertIdentifierId值,但它沒有顯示。任何人都可以告訴我我的映射是否正確?任何人都可以告訴我問題是什麼以及如何解決它?如何解析sencha中的嵌套數組json響應值

Sample.Json

{ 
     "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

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

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'  
     } 
    } 

}); 

app.js

var store = Ext.create('extcityview.store.ChildStore', { 

           model: "extcityview.model.ParentModel", 

}); 

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')); 

       }); 




       }); 
      } 
     }); 

感謝

回答

1

我注意到的第一件事情就是你的JSON似乎是在嵌套。我會首先改變......你所擁有的結構會讓生活變得比需要的更艱難。

JSON

{ 
    "parents": [ 
     { 
      "children": [ 
       { 
        "childId": 7, 
        "childdetails": [ 
         { 
          "childName": "Test12", 
          "childAge": "25" 
         }, 
         { 
          "childName": "Test13", 
          "childAge": "21" 
         } 
        ] 
       }, 
       { 
        "childId": 8, 
        "childdetails": [ 
         { 
          "childName": "Test14", 
          "childAge": "26" 
         }, 
         { 
          "childName": "Test15", 
          "childAge": "27" 
         } 
        ] 
       } 
      ], 
      "parentName": "secondname", 
      "parentAge": "35" 
     } 
    ] 
}  

您應該能夠擺脫你ChildrenModel,並有ChildModel屬於ParentModel。此外,您可以將其hasMany添加到ChildModel,因爲它是涉及ChildDetails模型的「childdetails」。

我總是發現這個傢伙ExtJs hasMany relationship rules刷新我的記憶是有幫助的。