Ext JS的4:转换JSON对象来使用JavaScript另一个JSON对象(Ext JS 4:

2019-09-20 14:54发布

什么是使用JavaScript JSON皈依JSON B中的最简单的方法?

JSON答:

{
    "d":
    [
        {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"0","value":"one"},
        {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"1","value":"two"},
        {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"2","value":"three"}
    ]
}

JSON B:

{
    data:
    [
        {"key":"1", "value":"one"},
        {"key":"2", "value":"two"},
        {"key":"3", "value":"three"}
    ]
}    

===================

使用Ext JS的和你有一个ASP.NET代理时,2012年8月1日更新(回答:

我没有在我的问题关于我使用什么JavaScript框架提供了这一点,但事实证明,你可以通过隐在root属性指定的值“d”消除“d”键

var statusDropdownStore = new Ext.data.Store({
    proxy: new Ext.ux.AspWebAjaxProxy({
        url: '/track/Controls/Shared/GeneralService.asmx/GetDropdownOptions',
        actionMethods: {
            create: 'POST',
            destroy: 'DELETE',
            read: 'POST',
            update: 'POST'
        },
        extraParams: {
            user_login: authUser,
            table_name: '[status]'
        },
        reader: {
            type: 'json',
            model: 'DropdownOption',
            root: 'd'
        },
        headers: {
            'Content-Type': 'application/json; charset=utf-8'
        }
    })
});

代理:

Ext.define('Ext.ux.AspWebAjaxProxy', {
    extend: 'Ext.data.proxy.Ajax',
    require: 'Ext.data',

    buildRequest: function (operation) {
        var params = Ext.applyIf(operation.params || {}, this.extraParams || {}),
                                request;
        params = Ext.applyIf(params, this.getParams(params, operation));
        if (operation.id && !params.id) {
            params.id = operation.id;
        }

        params = Ext.JSON.encode(params);

        request = Ext.create('Ext.data.Request', {
            params: params,
            action: operation.action,
            records: operation.records,
            operation: operation,
            url: operation.url
        });
        request.url = this.buildUrl(request);
        operation.request = request;
        return request;
    }
});

组合框(下拉列表)配置:

                    {
                        xtype: 'combo',
                        fieldLabel: 'Status',
                        emptyText: 'Select a status...',
                        store: statusDropdownStore,
                        valueField: 'key',
                        displayField: 'value',
                        mode: 'remote',  // or 'local'
                        renderTo: document.body
                    },

Answer 1:

我认为这将做到这一点:

var TheJsonA = JSON.parse(JsonA);
TheJsonA = TheJsonA.d;

var TheJsonB = {};
TheJsonB.data = [];
var TheObject = {};

if (TheJsonA.length > 0) { 

  for (var i = 0, LoopTimes = TheJsonA.length; i < LoopTimes; i++) {
      TheObject = {};
      TheObject.key = TheJsonA[i].key;
      TheObject.value = TheJsonA[i].value;
      TheJsonB.data.push(TheObject);
  }
}

TheJsonA = null; // if you need to discard the initial object

我还这个JsonB不应该是一个包含对象数组的对象; 我认为这应该只是这样的对象数组:

[
     {"key":"1", "value":"one"},
     {"key":"2", "value":"two"},
     {"key":"3", "value":"three"}
]  


Answer 2:

下面是一个示例

var old = {
    "d":
    [
        {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"0","value":"one"},
        {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"1","value":"two"},
        {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"2","value":"three"}
    ]
};

old.data = old.d;
delete old.d;
for(var i=0,l=old.data.length;i<l;i++){
    delete old.data[i].__type;
}


Answer 3:

你可以尝试在列出的解决方案https://stackoverflow.com/a/1219633/832457 -使用delete删除键。

尝试通过数组循环和使用delete ,然后重命名阵列(通过创建一个新的属性和删除旧



Answer 4:

试试这个:) http://jsfiddle.net/daewon/LnpXb/

var jsonA = {
    d: [
        {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"0","value":"one"},
        {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"1","value":"two"},
        {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"2","value":"three"}
   ]
};

var jsonB = {
    data: []
};

var d = jsonA.d;
for (var i=0; i<​d.length​; i++){
    var obj = {
        key : d[i].key,
        value : d[i].value
    };

    jsonB.data.push(obj);    
}
console.log(JSON.stringify(jsonB));
=> {"data":[{"key":"0","value":"one"},{"key":"1","value":"two"},{"key":"2","value":"three"}]} 



文章来源: Ext JS 4: Convert JSON object to another JSON object using JavaScript