0

我无法解析我的嵌套 json 数据,我尝试了很多方法,但我无法成功。任何帮助表示赞赏。这是我的 json 输出如下所示:

[
  {
    "task": {
      "locator": "FGWESD",
      "subtask": [
        {
          "work": {
            "number": "1145",
            "id": "0",
            "status": true,
            "gate": "N\/A",
            },
          "sequenceNumber": "0",
          "id": "0"
        },
        {
          "work": {
            "number": "1145",
            "id": "0",
            "status": true,
            "gate": "N\/A",
          },
          "sequenceNumber": "0",
          "id": "0"
        }
      ],
      "connectTime": "0",
      "id": "0"
    }
  }
]

这是我的模型:

Ext.define('MyApp.model.MyModel',{
    extend:'Ext.data.Model',
    xtype:'myModel',
    config:{
        fields:[
        {name:'number',mapping:'work.number'},
        {name:'id',mapping:'work.id'},
        {name:'locator',mapping:'task.locator'},
        {name:'gate',mapping:'work.gate'}

        ]
    }
});

这里是商店:

Ext.define('MyApp.store.StoreList', {
    extend:'Ext.data.Store',
     config:{
        model:'MyApp.model.MyModel',
        storeId: 'id_Store',
// added the url dynamically inside the controller
        proxy:{
            type:'ajax',
            reader:
            {
                type:"json",
                rootProperty: 'subtask'
            },
            method: 'POST',
            actionMethods: {
                create : 'POST',
                read   : 'POST', // by default GET
                update : 'POST',
                destroy: 'POST'
            },
            headers :{
                "Content-Type" :'application/xml',
                'Accept':'application/json'
            }

        }
    }
});

这是我的控制器代码:

    Ext.define('MyApp.controller.LoginController', {
        extend: 'Ext.app.Controller',
        requires: ['Ext.data.proxy.Rest'],

        config: {
// My code is too long to add here so am adding store loading when user taps login button

},
  getDetails: function(){
        var segmentStore = Ext.create('MyApp.store.StoreList');
        var url = 'http://localhost:8080/apps';
        segmentStore.getProxy().setUrl(url.trim());
        segmentStore.load({
                   scope:this,
                    callback: function(records, operation, success){
                        if(success){
                           console.log('records: ',records);

                           console.log('records: '+records.length); // prints here 1
                          console.log('locator: '+records[0].getData().locator);
// prints FGWESD
                          console.log('locator: '+records[0].getData().number);
//prints undefined
// 
                        }
}
            }
        )
    },
});

任何人都可以帮助我。如何获取数字、门、ID 和状态的值?必须对模型、存储和控制器进行哪些必要的更改?请帮我解决?谢谢。

4

2 回答 2

3

正如我在评论中所写,如果不手动解析数据并将其加载到商店,我认为您无法实现这一目标。所以 getDetails 函数应该是这样的:

getDetails: function(){
    var segmentStore = Ext.create('MyApp.store.StoreList');
    Ext.Ajax.request({
        url: 'http://localhost:8080/apps',
        success: function(response){
            var responseObj = Ext.decode(response.responseText);
            var task = responseObj[0].task;
            var locator = task.locator;
            var subtasks = [];
            Ext.each(task.subtask, function(subtask) {
                subtasks.push({
                    number: subtask.work.number,
                    id: subtask.work.id,
                    gate: subtask.work.gate,
                    locator: locator
                });
            });
            segmentStore.setData(subtasks);
        }
    });
}

此外,使用此方法时,您应该从模型中删除映射,并且可以摆脱存储的代理定义。另外,我不确定你为什么要在“getDetails”中创建商店而不是在控制器的“商店”配置中定义它,但也许你有你的理由..我没有运行代码,所以可能有错误,但我希望你明白。

于 2013-06-06T19:14:22.793 回答
0

我认为您商店的根属性应该是:

rootProperty: 'task.subtask'
于 2013-06-06T18:29:16.307 回答