2011-11-23 103 views
3

我無法從複雜的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

回答

4

您可以使用網上JSON解析器(http://json.parser.online.fr/)來解析從解析器數據JSON XML您輕鬆seprate對象和數組,你會得到哪些是需要you..i希望你