Sencha Touch:如何从复杂的JSON对象中获取数据

问题描述:

我无法从复杂的json对象中检索记录,但是我能够在使用TPL时获取数据。Sencha Touch:如何从复杂的JSON对象中获取数据

请看下面的例子代码:

JSON:

{ 
"lists": [ 
    { 
     "title": "Groceries", 
     "id": "1", 
     "items": [ 
      { 
       "text": "contact solution - COUPON", 
       "listId": "1", 
       "id": "4", 
       "leaf": "true" 
      }, 
      { 
       "text": "Falafel (bulk)", 
       "listId": "1", 
       "id": "161", 
       "leaf": "true" 
      }, 
      { 
       "text": "brita filters", 
       "listId": "1", 
       "id": "166", 
       "leaf": "false" 
      } 
     ] 
    } 
] 

型号:

Ext.regModel("TopModel", 
{ 
    fields: [ 
     { name: 'title', type: 'string' }, 
     { name: 'id', type: 'string' }, 
    ], 
    hasMany: [ 
     { model: 'SubModel', name: 'items' } 
    ], 
    proxy: { 
    type: 'ajax', 
    url : 'test/lists.json', 
     actionMethods: { 
      create: 'POST', 
      destroy: 'POST', 
      read: 'POST', 
      update: 'POST' 
    }, 
    reader: { 
     type: 'json', 
     root: function(data) { 
        return data.lists || []; 
       } 
    } 
    } 
}); 

Ext.regModel("SubModel", 
{ 
    fields: [ 
      { name: 'text', type: 'string'}, 
     { name: 'listId', type: 'string'}, 
     { name: 'id', type: 'string'}, 
     { name: 'leaf', type: 'string'} 
    ] 
}); 

我认为文件中,我已经定义商店为下面。

this.stores = new Ext.data.Store({ 
      clearOnPageLoad: false, 
     autoLoad:true, 
      model:'TopModel', 
     getGroupString: function(record) { 
     return record.get('leaf'); 
      } 
}); 
}); 

我不能检索值record.get(“叶”),因为这表示孩子模型项目。当我试图打印它时,它打印为未定义。

如何访问子属性?这里'项目'被列为一个数组。

我试图显示数据使用列表如下。所有记录都显示出来,但问题在于它被选为一个整体项目,而不是每个项目的单独列表。

var list = new Ext.List({ 
    cls: 'big-list', 
    grouped : true, 
    emptyText: '<div>No data found</div>', 
    itemTpl: ['<div><tpl for="items">', 
     '<div>', 
     '{id} {text}', 
     '</div>', 
     '</tpl></div>', 
     ], 
    store: this.stores, 
    listeners: { 
     itemtap: this.onListItemTap, 
     scope: this 
    } 
}); 

请帮助我如何让列表项目显示为单个记录。

+0

你想这帮助所有数据显示按“叶”分组的所有TopModel或所有子模型? this.stores根据您的代码“TopModel”存储,但您想显示SubModels。 感谢您获取更多信息。 – Philipp

+0

整个模板在您的案例中被视为“项目”。 您也可以将root设置为'lists'而不是函数 – Dawesi

您可以使用网上JSON解析器(http://json.parser.online.fr/)来解析从解析器数据JSON XML您轻松seprate对象和数组,你会得到哪些是需要you..i希望你