Javascript 从多维json获取数据
我有这个jsonJavascript 从多维json获取数据,javascript,json,extjs,sencha-touch-2,Javascript,Json,Extjs,Sencha Touch 2,我有这个json { "root":{ "category":[ { "categoryId":"1", "children":{ "child":[ { "subcategoryId":"1", "children":{
{
"root":{
"category":[
{
"categoryId":"1",
"children":{
"child":[
{
"subcategoryId":"1",
"children":{
"child":[
{
"productId":"1"
},
{
"productId":"2"
}
]
}
},
{
"subcategoryId":"2",
"children":{
"child":[
{
"productId":"3"
},
{
"productId":"4"
}
]
}
},
{
"subcategoryId":"3",
"children":{
"child":[
{
"productId":"5"
},
{
"productId":"6"
}
]
}
}
]
}
},
{
"categoryId":"2",
"children":{
"child":[
{
"subcategoryId":"4",
"children":{
"child":[
{
"productId":"7"
},
{
"productId":"8"
}
]
}
},
{
"subcategoryId":"5",
"children":{
"child":[
{
"productId":"9"
},
{
"productId":"10"
},
{
"productId":"11"
}
]
}
}
]
}
}
]
}
}
这个模型
Ext.define('ParentMode', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'categoryId', type: 'string'},
{name: 'children'} // VERY IMPORTANT TO ADD THIS FIELD
],
proxy: {
type: 'ajax',
url : 'store.php',
reader: {
type: 'json',
rootProperty: 'root.category' // this works fine
}
}
}
})
我有3个不同的视图:第一个视图“main”,其中我有一个数据视图,其中我显示“categoryId”:
var myStore = Ext.create('Ext.data.Store', {
model: 'ParentMode',
autoLoad:true
});
var dataview = Ext.create('Ext.DataView', {
scrollable: false,
store: myStore,
listeners: {
itemtap: function(list, index, target, record){
this.up('main').push({
xtype: 'categories',
title: record.get('categoryId'),
extraRecord:record
})
}
}
});
从这个“主”视图可以看到,我推一个新视图“类别”,当点击一个项目时,我给它分配一个新标题“categoryId”,现在一切都很好。
现在在“类别”视图中,我以以下方式显示所有“子类别ID”:
var dataview = Ext.create('Ext.dataview.DataView', {
store: myStore,
itemTpl: '<tpl for="children.child"><div class="category-thumb">SubcategoryID: {subcategoryId}</div></tpl>',
...
问题是
title: record.get('subcategoryId'),
不起作用,我想是因为它嵌套在json中,我想我必须做一些循环,但我不知道如何做
同样的问题也出现在“产品”中,我怎样才能得到“产品SID”
title: record.get('subcategoryId'),