如何从另一种JSON格式中获取一种JSON对象格式

时间:2018-08-19 06:21:40

标签: javascript json

也许这个问题已经在某个地方被问到并回答了,但是搜索了3个多小时后,我问的是这个问题。

以下是我的JSON数据

var my_data = [
    {
        "TempRture_qc": 4,
        "VoltAGE": 44.09722,
        "TempRture": 22.32,
        "VoltAGE_qc": 55,
        "_time": "2018-08-07T03:39:29.001Z"
    },
    {
        "TempRture_qc": 2,
        "VoltAGE": 42.09722,
        "TempRture": 22.12,
        "VoltAGE_qc": 0,
        "_time": "2018-08-07T03:39:30.006Z"
    },
    {
        "TempRture_qc": 1,
        "VoltAGE": 43.09722,
        "TempRture": 22.82,
        "VoltAGE_qc": 0,
        "_time": "2018-08-07T03:39:31.009Z"
    }
];

我需要的期望输出

[
  {
    "name": "TempRture_qc",
    "data": [
    {"name":"2018-08-07T03:39:29.001Z","y":4},
    {"name":"2018-08-07T03:39:30.006Z","y":2},
    {"name":"2018-08-07T03:39:33.017Z","y":1}
    ]
  },
  {
    "name": "VoltAGE",
    "data": [
    {"name":"2018-08-07T03:39:29.001Z","y":44.09722},
    {"name":"2018-08-07T03:39:30.006Z","y":42.09722},
    {"name":"2018-08-07T03:39:33.017Z","y":43.09722}
    ]
  },
  {
    "name": "TempRture",
    "data": [
    {"name":"2018-08-07T03:39:29.001Z","y":22.32},
    {"name":"2018-08-07T03:39:30.006Z","y":22.12},
    {"name":"2018-08-07T03:39:33.017Z","y":22.82}
    ]
  },
  {
    "name": "VoltAGE_qc",
    "data": [
    {"name":"2018-08-07T03:39:29.001Z","y":55},
    {"name":"2018-08-07T03:39:30.006Z","y":0},
    {"name":"2018-08-07T03:39:33.017Z","y":0}
    ]
  }

]

为了获得上面的输出,我尝试了下面的代码。

var accounting = [];
var fieldName = {};

for (var x in obj){

    var mykey = Object.keys(obj[x]);
    for (var mk in mykey){      

        if(mykey[mk]=='VoltAGE'){

            fieldName.name = mykey[mk];
            // accounting.push({
            //     "name":mykey[mk]
            // }) 
       }
       if(mykey[mk]=='TempRture'){       
         fieldName.name = mykey[mk];        
       }
      // console.log(mykey[mk]); //to get the key name
    }

    accounting.push({ 
        "name" : obj[x]._time,
        "y"  : obj[x][employees.name],
    })

    fieldName.data = accounting;

  }
  console.log(fieldName );

这样做,我得到的就是JSON以下

{ name: 'TempRture',
  data:
   [ { name: '2018-08-07T03:39:29.001Z', y: 22.32 },
     { name: '2018-08-07T03:39:32.014Z', y: 22.12 },
     { name: '2018-08-07T03:39:33.017Z', y: 22.82 } ] }

我无法理解如何在一个JSON对象中获取数据。

3 个答案:

答案 0 :(得分:2)

对于时间复杂度较低的解决方案,请尝试.reduce进入由内部对象的键索引的对象,如果该累加器中尚不存在此键,则在该键处创建一个{ name, data: [] } 。然后,推送到data数组,并获取整个对象的值:

var my_data=[{"TempRture_qc":4,"VoltAGE":44.09722,"TempRture":22.32,"VoltAGE_qc":55,"_time":"2018-08-07T03:39:29.001Z"},{"TempRture_qc":2,"VoltAGE":42.09722,"TempRture":22.12,"VoltAGE_qc":0,"_time":"2018-08-07T03:39:30.006Z"},{"TempRture_qc":1,"VoltAGE":43.09722,"TempRture":22.82,"VoltAGE_qc":0,"_time":"2018-08-07T03:39:31.009Z"}]

console.log(Object.values(
  my_data.reduce((a, { _time, ...obj }) => {
    Object.entries(obj).forEach(([name, val]) => {
      if (!a[name]) a[name] = { name, data: [] };
      a[name].data.push({ name: _time, y: val });
    });
    return a;
  }, {})
));

答案 1 :(得分:2)

var my_data=[{"TempRture_qc":4,"VoltAGE":44.09722,"TempRture":22.32,"VoltAGE_qc":55,"_time":"2018-08-07T03:39:29.001Z"},{"TempRture_qc":2,"VoltAGE":42.09722,"TempRture":22.12,"VoltAGE_qc":0,"_time":"2018-08-07T03:39:30.006Z"},{"TempRture_qc":1,"VoltAGE":43.09722,"TempRture":22.82,"VoltAGE_qc":0,"_time":"2018-08-07T03:39:31.009Z"}]

var keys = Object.keys(my_data[0])
var result= [];
for(i = 0; i<keys.length-1; i++) {
   var obj = {name: keys[i],data: []}
   obj.data = my_data.map(val=>({name: val["_time"], y: val[keys[i]]}));
   result.push(obj);
}
console.log(result)

答案 2 :(得分:1)

使用mapfindIndexforEach函数的答案是可以理解的

var my_data = [{ "TempRture_qc": 4, "VoltAGE": 44.09722, "TempRture": 22.32, "VoltAGE_qc": 55, "_time": "2018-08-07T03:39:29.001Z" }, { "TempRture_qc": 2, "VoltAGE": 42.09722, "TempRture": 22.12, "VoltAGE_qc": 0, "_time": "2018-08-07T03:39:30.006Z" }, { "TempRture_qc": 1, "VoltAGE": 43.09722, "TempRture": 22.82, "VoltAGE_qc": 0, "_time": "2018-08-07T03:39:31.009Z" } ],
  result = [];

my_data.map(itm => {
  let keys = Object.keys(itm);
  keys.forEach(iitt => {
    if (iitt != '_time') {
      let index = result.findIndex(ii => {
        return ii.name == iitt;
      })
      if (index == -1) {
        result.push({
          name: iitt,
          data: []
        });
        result[result.length - 1].data.push({
          name: itm["_time"],
          y: itm[iitt]
        })

      } else {
        result[index].data.push({
          name: itm["_time"],
          y: itm[iitt]
        });
      }
    }
  })
})

console.log(result)