如何解析Extjs 6.5.2中的Json二维数组?

时间:2017-08-30 02:15:47

标签: extjs

我想在二维数组中获取所有值,我确信我在模型配置中做了一些事情。我怎么能这样做?

这是我商店的代码:

Ext.define('Test.store.PathStore', {
extend: 'Ext.data.Store',
alias: 'store.PathStore',
storeId:'PathStore',
model : 'Test.model.PathModel',
autoSync:true,
proxy: {
    type: 'ajax',
    url:"url",
    method:'GET',
    reader: {
        type: 'json',
        rootProperty : ''
    }
}
});

以下是我的模型的代码:

Ext.define('Test.model.PathModel', {
extend: 'Ext.data.Model',

fields: [
    {name:'CID', type:'auto'},
    {name:'NAME', type:'auto'},
    {name:'DEFAULT_NAME', type:'auto'},
    {name:'REPRESENTATIONS', type:'auto'}
    ]
});

这是我的控制器的代码:

onButtonClick: function (selModel, record, index, options) {

    var pathStore = Ext.getStore('PathStore');
    pathStore.load({
        // Some params,
        callback: function(records, success, response, options) {
            if(success){
            var arr = Object.values(records[0].getData().DEFAULT_NAME);
            console.log(records);
            console.log(arr);
            console.log(records[0].getData());
          //Something  I Have To Do 
            }
}
scope: this
});
}

这是我的Json的格式:

[
  [
  {
      "CID": 111111,
      "NAME": null,
      "DEFAULT_NAME": "Hello guys",
      "REPRESENTATIONS": null,
      "ALL_REPRESENTATIONS": [
        {
          "cid": 111111,
          "Name": "Hello",
          "DefaultName": "guys",
        }
      ]
    }
  ],
  [
    {
      "CID": 2222222,
      "NAME": null,
      "DEFAULT_NAME": "Hello World",
      "REPRESENTATIONS": null,
      "ALL_REPRESENTATIONS": [
        {
          "cid": 22222222,
          "Name": "Hello",
          "DefaultName": "World",
        }
      ]
    }
  ]
]

Json Format

我在DEFAULT_NAMENAMECID中获得[object Object]或其他内容。我该怎么做?

提前致谢, 本

2 个答案:

答案 0 :(得分:0)

如果您的模型中有一对多关系,请在模型配置中使用'hasMany'属性:

Ext.define("Test.model.RepresentationModel", {
    extend: 'Ext.data.Model',
    fields: [
        'cid', 'Name', 'DefaultName'
    ],
    belongsTo: 'Test.model.PathModel'
});

Ext.define('Test.model.PathModel', {
    extend: 'Ext.data.Model',

    fields: [
        {name:'CID', type:'auto'},
        {name:'NAME', type:'auto'},
        {name:'DEFAULT_NAME', type:'auto'},
        {name:'REPRESENTATIONS', type:'auto'}
    ],

    hasMany: {model: 'Test.model.RepresentationModel', name: 'ALL_REPRESENTATIONS'},
});

答案 1 :(得分:0)

One way to get the data from the nested array is to use the reader's transform configuration.

It is passed the raw (deserialized) data object. The transform function returns a data object, which can be a modified version of the original data object, or a completely new data object.

Get-Content $PCList | ForEach-Object {
    $pc = $_
    if (-not (Test-Connection -ComputerName $pc -BufferSize 16 -Count 2 -Quiet)) {
        New-Object -Type PSObject -Property @{
            'Hostname'  = $pc
            'Date'      = $null
            'Operation' = $null
            'Status'    = 'Connection failed'
            'Title'     = $null
        }
    } else {
        Invoke-Command -ComputerName $pc -ScriptBlock {
            ...
        }
    }
} | Out-GridView

This way you get rid of the enclosing array and normalize the data.

Here is a working fiddle: https://fiddle.sencha.com/#view/editor&fiddle/25ut