Arrays 转换为prper格式

Arrays 转换为prper格式,arrays,format,json,Arrays,Format,Json,如何将以下json转换为所需格式 我所拥有的: { "1":{ "name":"Room1", "agegroup":"38 to 67", "display_agegroup":"Months", "0":"After School Care" }, "5":{ "name":"new room", "agegroup":"3.08 to 4.", "display_agegroup":"Years", "0"

如何将以下json转换为所需格式

我所拥有的:

{
  "1":{
    "name":"Room1",
    "agegroup":"38 to 67",
    "display_agegroup":"Months",
    "0":"After School Care"
  },
  "5":{
     "name":"new room",
     "agegroup":"3.08 to 4.",
     "display_agegroup":"Years",
     "0":"After School Care",
     "1":"Before School Care"
  }
}
我需要的是:

[{
   "id":"1",
   "field1":"Room1",
   "field2":"38 to 67",
   "field3":"Months",
   "field4":"After School Care"},
 {
   "id":"5",
   "field1":"new room",
   "field2":"3.08 to 4.",
   "field3":"Years",
   "field4":"After School Care & Before School Care"
}]
请引导我

var out = [];
var input = {
  "1": { /* ... */ },
  "5": { /* ... */ }
};

_.each(_.keys(input), function(el) {
  var obj = {
    "id": el
  };

  _.each(_.keys(input[el], function(k, idx) {
    obj["field"+(1+idx)] = input[el][k];
  });

  out.push(obj);
});

为方便起见,此使用。如果您不能/不会,那么用标准JS代码替换0.key和0.key是非常简单的。

您尝试过什么吗?提示:使用。尝试过,但没有结果。