关于ajax:Ext JS 4:使用JavaScript将JSON对象转换为另一个JSON对象

Ext JS 4: Convert JSON object to another JSON object using JavaScript

使用JavaScript将JSON A转换为JSON B最简单的方法是什么?

JSON:

1
2
3
4
5
6
7
8
{
   "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"}
    ]
}

JSONB:

1
2
3
4
5
6
7
8
{
    data:
    [
        {"key":"1","value":"one"},
        {"key":"2","value":"two"},
        {"key":"3","value":"three"}
    ]
}

(=)

2012年8月1日更新(使用ext js时回答,您有一个ASP.NET代理:

在我的问题中,我没有提供这个关于我在javascript框架中使用的是什么,但事实证明,通过在根属性中指定值"d",可以隐式地消除"d"键。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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'
        }
    })
});

代理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
    }
});

组合框(下拉)配置:

1
2
3
4
5
6
7
8
9
10
                    {
                        xtype: 'combo',
                        fieldLabel: 'Status',
                        emptyText: 'Select a status...',
                        store: statusDropdownStore,
                        valueField: 'key',
                        displayField: 'value',
                        mode: 'remote',  // or 'local'
                        renderTo: document.body
                    },

这是一个样本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var old = {
   "d":
    [
        {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"0","value":"one
<hr>
<p>
try this :)
http://jsfiddle.net/daewon/LnpXb/
</p>

[cc lang="
javascript"]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"}]}


I think this would do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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不应该是包含对象数组的对象;我认为它应该只是一个这样的对象数组:

[cc lang="javascript"][
{"key":"1","value":"one


您可以尝试在https://stackoverflow.com/a/1219633/832457中概述的解决方案-使用delete删除密钥。

尝试在数组中循环并使用delete,然后重命名数组(通过创建新属性并删除旧属性