转置 JSON 数据

Transposing JSON data

我知道有关于此的话题,但我不明白,我的 json 文件可能需要帮助。我想将 json 绑定到我的 SAPUI5 控件之一,但我没有将其转换。

问题是,我的数据已经在前端,所以我不想进行第二次后端调用,我需要转置我的 json 数据。

这是我得到的,列是 Mon01 .. 等等

    Mon01   Mon02   Mon03   Mon04
0   03/2015 04/2015 05/2015 06/2015
1   1       3       5       21
2   2       4       6       22
3   10      11     12       23

这就是我想要的:

    Mon01   Mon02   Mon03   Mon04
0   03/2015 1       2       10
1   04/2015 3       4       11
2   05/2015 5       6       12
3   06/2015 21      22      23

或者在 JSON 我得到的是:

d : 
   [
      0: {
         Mon01: "03/2015",
         Mon02: "04/2015",
         Mon03: "05/2015",
      },
      1: {
         Mon01: "1,0",
         Mon02: "3,0",
         Mon03: "5,0",
 },
      2: {
         Mon01: "2,0",
         Mon02: "4,0",
         Mon03: "6,0",
         Mon04: "",
         Mon05: "",
         Mon06: "",
 },
      3: {
         Mon01: "10,0",
         Mon02: "11,0",
         Mon03: "12,0",
         Mon04: "",
         Mon05: "",
         Mon06: "",
 },
      length: 3
   ]

在JSON中我想要什么:

d : 
   [
      0: {
         Mon01: "03/2015",
         Mon02: "1,0",
         Mon03: "2,0",
         Mon04: "3,0",
         Mon05: "",
         Mon06: "",
     },
      1: {
         Mon01: "04/2015",
         Mon02: "3,0",
         Mon03: "4,0",
         Mon04: "11,0",
         Mon05: "",
         Mon06: "",
 },
      2: {
         Mon01: "05/2015",
         Mon02: "5,0",
         Mon03: "6,0",
         Mon04: "12,0",
         Mon05: "",
         Mon06: "",
 },
      length: 3
   ]

我所做的并不是最好的(但有效)移动每一行的单一方法:

transpose : function(oTreeAll) {

    var oTreeNew = new Array();
    var oTreeSingle = {};

oTreeSingle.Mon01 = oTreeAll[0].Mon01.replace(',', '.');
oTreeSingle.Mon02 = oTreeAll[1].Mon01.replace(',', '.');
oTreeSingle.Mon03 = oTreeAll[2].Mon01.replace(',', '.');
oTreeSingle.Mon04 = oTreeAll[3].Mon01.replace(',', '.');
oTreeNew.push(oTreeSingle);

var oTreeSingle1 = {};
oTreeSingle1.Mon01 = oTreeAll[0].Mon02.replace(',', '.');
oTreeSingle1.Mon02 = oTreeAll[1].Mon02.replace(',', '.');
oTreeSingle1.Mon03 = oTreeAll[2].Mon02.replace(',', '.');
oTreeSingle1.Mon04 = oTreeAll[3].Mon02.replace(',', '.');
oTreeNew.push(oTreeSingle1);

var oTreeSingle2 = {};
oTreeSingle2.Mon01 = oTreeAll[0].Mon03.replace(',', '.');
oTreeSingle2.Mon02 = oTreeAll[1].Mon03.replace(',', '.');
oTreeSingle2.Mon03 = oTreeAll[2].Mon03.replace(',', '.');
oTreeSingle2.Mon04 = oTreeAll[3].Mon03.replace(',', '.');
oTreeNew.push(oTreeSingle2);

var data = {
    "d" : {
        "results" : oTreeNew,
    }
};

    return data;
},

你会怎么做?我已经阅读了很多关于地图功能的文章,但我不了解其工作方式或如何使其适应我的 json,任何人都可以帮助我吗?非常感谢!


感谢 Shibaji Chakraborty,我得到了 json:

的解决方案
transpose : function(oTreeAll) {

    var keys = [];
    var i = 0;
    for ( var key in oTreeAll[i]) {
        keys.push(key);
    }

    console.log(keys);

    var newObj = {
        d : []
    };

    newObj['length'] = oTreeAll.length;
    for (var k = 0; k < oTreeAll.length; k++) {
        var obj = {};
        for ( var cnt in keys) {
            obj[keys[cnt]] = "";
        }
        newObj.d.push(obj);
    }

    for (var k = 0; k < oTreeAll.length; k++) {
        for (var j = 0; j < oTreeAll.length; j++) {
            newObj.d[k][keys[j]] = oTreeAll[j][keys[k]];
        }
    }

    console.log(newObj);
            return newObj;

},

请查找以下代码:

var data = {d : 
[
  {
     Mon01: "03/2015",
     Mon02: "04/2015",
     Mon03: "05/2015",
  },
  {
     Mon01: "1,0",
     Mon02: "3,0",
     Mon03: "5,0",
  },
  {
     Mon01: "2,0",
     Mon02: "4,0",
     Mon03: "6,0",
     Mon04: "",
     Mon05: "",
     Mon06: "",
  },
  {
     Mon01: "10,0",
     Mon02: "11,0",
     Mon03: "12,0",
     Mon04: "",
     Mon05: "",
     Mon06: "",
}
],
        length: 3};

//console.log(data.d[0]);
var keys = [];
for(var key in data.d[data.length]){
   //console.log(key);
  keys.push(key);
}

var newObj = {d:[]};
newObj['length'] = data.length;
for(var k =0;k<data.length;k++){
  var obj = {};
  for(var cnt in keys){
    obj[keys[cnt]] = "";
  }
  newObj.d.push(obj);
 }
for(var k =0;k<data.length;k++){
   //var obj = {};
  //console.log(k);
  for(var j=0;j<data.length;j++){
    newObj.d[k][keys[j]] = data.d[j][keys[k]];
  }
}
console.log(newObj);