Ext JS的4:JSON对象转换为使用JavaScript另一个JSON对象 [英] Ext JS 4: Convert JSON object to another JSON object using JavaScript

查看:108
本文介绍了Ext JS的4:JSON对象转换为使用JavaScript另一个JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

JSON答:

  {
    D:
    [
        {__type:Web.Controls.Shared.GeneralService + DropdownKeyValuePair,重点:0,值:一},
        {__type:Web.Controls.Shared.GeneralService + DropdownKeyValuePair,钥匙:1,值:二},
        {__type:Web.Controls.Shared.GeneralService + DropdownKeyValuePair,重点:2,值:三}
    ]
}
 

JSON B:

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

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

使用Ext JS的当

2012年8月1日更新(答案和你有一个ASP.NET代理:

我没有提供这个在我的问题有关,我使用了JavaScript框架是什么,但事实证明,你可以通过在root属性指定值D

  VAR statusDropdownStore =新Ext.data.Store({
    代理:新Ext.ux.AspWebAjaxProxy({
        网址:/track/Controls/Shared/GeneralService.asmx/GetDropdownOptions,
        actionMethods:{
            创建:POST,
            破坏:删除,
            阅读:POST,
            更新:POST
        },
        extraParams:{
            USER_LOGIN:AUTHUSER,
            TABLE_NAME:[状态]
        },
        读者: {
            类型:JSON,
            型号:DropdownOption,
            根:D
        },
        标题:{
            内容类型:应用/ JSON的;字符集= UTF-8
        }
    })
});
 

代理

  Ext.define(Ext.ux.AspWebAjaxProxy',{
    延伸:Ext.data.proxy.Ajax,
    要求:Ext.data,

    buildRequest:功能(操作){
        变种PARAMS = Ext.applyIf(operation.params || {},this.extraParams || {}),
                                请求;
        PARAMS = Ext.applyIf(参数,可以this.getParams(参数,可以运行));
        如果(operation.id&安培;&安培;!params.id){
            params.id = operation.id;
        }

        PARAMS = Ext.JSON.en code(PARAMS);

        请求= Ext.create('Ext.data.Request',{
            params:一个参数,可以
            动作:operation.action,
            记录:operation.records,
            操作:操作,
            网址:operation.url
        });
        request.url = this.buildUrl(要求);
        operation.request =请求;
        返回请求;
    }
});
 

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

  {
                        的xtype:'二合一',
                        fieldLabel:状态,
                        emptyText:选择一个状态......,
                        商店:statusDropdownStore,
                        valueField:钥匙,
                        displayField:'价值',
                        模式:'远程',//或本地
                        renderTo:document.body的
                    },
 

解决方案

我认为这将做到这一点:

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

这篇关于Ext JS的4:JSON对象转换为使用JavaScript另一个JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆