且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-08 11:45:25

我认为这将做到这一点:

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

变种TheJsonB = {};
TheJsonB.data = [];
变种TheObject = {};

如果(TheJsonA.length大于0){

  对于(VAR I = 0,LoopTimes = TheJsonA.length; I< LoopTimes;我++){
      TheObject = {};
      TheObject.key = TheJsonA [I]。关键;
      TheObject.value = TheJsonA [I]。价值;
      TheJsonB.data.push(TheObject);
  }
}

TheJsonA = NULL; //如果您需要放弃最初的对象
 

我也是这个JsonB不应该是一个对象,包含对象的数组;我想这应该只是对象的数组是这样的:

  [
     {关键:1,值:一},
     {关键:2,值:二},
     {关键:3,值:三}
]
 

What is the easiest way to convert JSON A to JSON B using JavaScript?

JSON A:

{
    "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"}
    ]
}    

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

8/1/2012 update (answer when using Ext JS and you have an ASP.NET proxy:

I didn't provide this in my question about what I'm using for a JavaScript framework, but it turns out you can implicitly eliminate the "d" key by specifying the value "d" in the root property

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'
        }
    })
});

Proxy:

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;
    }
});

Combo Box (dropdown) configuration:

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

I think this would do it:

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

I also this JsonB shouldn't be an object that contains an array of objects; I think it should just be an array of objects like this:

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