使用 JavaScript 将 JSON A 转换为 JSON B 的最简单方法是什么?
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"}
]
}
====================
2012 年 8 月 1 日更新(使用 Ext JS 并且您有 ASP.NET 代理时回答:
我没有在关于我为 JavaScript 框架使用什么的问题中提供这一点,但事实证明,您可以通过在根属性中指定值“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
},