Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 3.99 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Ext JS 4: Convert JSON object to another JSON object using JavaScript
  2. {
  3.     "d":
  4.     [
  5.         {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"0","value":"one"},
  6.         {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"1","value":"two"},
  7.         {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"2","value":"three"}
  8.     ]
  9. }
  10.        
  11. {
  12.     data:
  13.     [
  14.         {"key":"1", "value":"one"},
  15.         {"key":"2", "value":"two"},
  16.         {"key":"3", "value":"three"}
  17.     ]
  18. }
  19.        
  20. var statusDropdownStore = new Ext.data.Store({
  21.     proxy: new Ext.ux.AspWebAjaxProxy({
  22.         url: '/track/Controls/Shared/GeneralService.asmx/GetDropdownOptions',
  23.         actionMethods: {
  24.             create: 'POST',
  25.             destroy: 'DELETE',
  26.             read: 'POST',
  27.             update: 'POST'
  28.         },
  29.         extraParams: {
  30.             user_login: authUser,
  31.             table_name: '[status]'
  32.         },
  33.         reader: {
  34.             type: 'json',
  35.             model: 'DropdownOption',
  36.             root: 'd'
  37.         },
  38.         headers: {
  39.             'Content-Type': 'application/json; charset=utf-8'
  40.         }
  41.     })
  42. });
  43.        
  44. Ext.define('Ext.ux.AspWebAjaxProxy', {
  45.     extend: 'Ext.data.proxy.Ajax',
  46.     require: 'Ext.data',
  47.  
  48.     buildRequest: function (operation) {
  49.         var params = Ext.applyIf(operation.params || {}, this.extraParams || {}),
  50.                                 request;
  51.         params = Ext.applyIf(params, this.getParams(params, operation));
  52.         if (operation.id && !params.id) {
  53.             params.id = operation.id;
  54.         }
  55.  
  56.         params = Ext.JSON.encode(params);
  57.  
  58.         request = Ext.create('Ext.data.Request', {
  59.             params: params,
  60.             action: operation.action,
  61.             records: operation.records,
  62.             operation: operation,
  63.             url: operation.url
  64.         });
  65.         request.url = this.buildUrl(request);
  66.         operation.request = request;
  67.         return request;
  68.     }
  69. });
  70.        
  71. {
  72.                         xtype: 'combo',
  73.                         fieldLabel: 'Status',
  74.                         emptyText: 'Select a status...',
  75.                         store: statusDropdownStore,
  76.                         valueField: 'key',
  77.                         displayField: 'value',
  78.                         mode: 'remote',  // or 'local'
  79.                         renderTo: document.body
  80.                     },
  81.        
  82. var TheJsonA = JSON.parse(JsonA);
  83. TheJsonA = TheJsonA.d;
  84.  
  85. var TheJsonB = {};
  86. TheJsonB.data = [];
  87. var TheObject = {};
  88.  
  89. if (TheJsonA.length > 0) {
  90.  
  91.   for (var i = 0, LoopTimes = TheJsonA.length; i < LoopTimes; i++) {
  92.       TheObject = {};
  93.       TheObject.key = TheJsonA[i].key;
  94.       TheObject.value = TheJsonA[i].value;
  95.       TheJsonB.data.push(TheObject);
  96.   }
  97. }
  98.  
  99. TheJsonA = null; // if you need to discard the initial object
  100.        
  101. [
  102.      {"key":"1", "value":"one"},
  103.      {"key":"2", "value":"two"},
  104.      {"key":"3", "value":"three"}
  105. ]
  106.        
  107. var old = {
  108.     "d":
  109.     [
  110.         {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"0","value":"one"},
  111.         {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"1","value":"two"},
  112.         {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"2","value":"three"}
  113.     ]
  114. };
  115.  
  116. old.data = old.d;
  117. delete old.d;
  118. for(var i=0,l=old.data.length;i<l;i++){
  119.     delete old.data[i].__type;
  120. }
  121.        
  122. var jsonA = {
  123.     d: [
  124.         {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"0","value":"one"},
  125.         {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"1","value":"two"},
  126.         {"__type":"Web.Controls.Shared.GeneralService+DropdownKeyValuePair","key":"2","value":"three"}
  127.    ]
  128. };
  129.  
  130. var jsonB = {
  131.     data: []
  132. };
  133.  
  134. var d = jsonA.d;
  135. for (var i=0; i<​d.length​; i++){
  136.     var obj = {
  137.         key : d[i].key,
  138.         value : d[i].value
  139.     };
  140.  
  141.     jsonB.data.push(obj);    
  142. }
  143. console.log(JSON.stringify(jsonB));
  144. => {"data":[{"key":"0","value":"one"},{"key":"1","value":"two"},{"key":"2","value":"three"}]}