【问题标题】:How to parse nested array json response value in sencha如何在 sencha 中解析嵌套数组 json 响应值
【发布时间】:2013-03-07 12:42:11
【问题描述】:

我正在尝试解析下面响应中的 childId 值。我无法解析该值。我是协会的新手,有很多概念。我在 app.js 中给出了一个警报 childId 值,但它没有显示。谁能告诉我我的映射是否正确?谁能告诉我问题是什么以及如何解决?

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

                 });




                }); 
            }
        });

谢谢

【问题讨论】:

    标签: extjs sencha-touch-2 extjs4.1 sencha-architect


    【解决方案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 一起刷新我的记忆很有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多