简体   繁体   English

单独的嵌套JSON对象

[英]Separate Nested JSON objects

So I'm probably overthinking this, but I have a JSON array with multiple levels of nested JSON objects that looks like this: 因此,我可能对此有些想法,但是我有一个带有多层嵌套JSON对象的JSON数组,如下所示:

var OldJSON =  
    {  
   "data":{  
      "shifts":{  
         "data":[  
            {  
               "id":1234,
               "entry_time":"2012-06-11 16:06:04",
               "attendance_id":1
            },
            {  
               "id":4321,
               "entry_time":"2014-07-23 17:29:04",
               "attendance_id":2
            }
         ]
      },
      "sites":{  
         "data":[  
            {  
               "id":4321,
               "name":"John Doe",
               "type":"field",
               "bases":{  
                  "data":[  
                     {  
                        "id":1234,
                        "type":"field",
                        "active":false
                     },
                     {  
                        "id":4321,
                        "type":"field",
                        "active":true
                     }
                  ]
               }
            },
            {  
               "id":1234,
               "name":"Jill Doe",
               "type":"field",
               "bases":{  
                  "data":[  
                     {  
                        "id":1234,
                        "type":"field",
                        "active":false
                     },
                     {  
                        "id":4321,
                        "type":"field",
                        "active":true
                     }
                  ]
               }
            }
         ]
      }
   }
}

What I want to do is Separate the 'base' object out of the 'sites' object and reorganize the JSON so it looks like this: 我要做的是将“基础”对象与“站点”对象分离,并重新组织JSON,因此它看起来像这样:

var NewJSON = 
{  
   "data":{  
      "shifts":{  
         "data":[  
            {  
               "id":1234,
               "entry_time":"2012-06-11 16:06:04",
               "attendance_id":1
            },
            {  
               "id":4321,
               "entry_time":"2014-07-23 17:29:04",
               "attendance_id":2
            }
         ]
      },
      "sites":{  
         "data":[  
            {  
               "id":4321,
               "name":"John Doe",
               "type":"field"
            },
            {  
               "id":1234,
               "name":"Jill Doe",
               "type":"field"
            }
         ]
      },
      "bases":{  
         "data":[  
            {  
               "id":1234,
               "type":"field",
               "active":false
            },
            {  
               "id":4321,
               "type":"field",
               "active":true
            }
         ]
      }
   }
}

I'm using Javascript and Angular in my project along with underscore. 我在下划线中在我的项目中使用Javascript和Angular。 So ideally the solution would utilize one of those three. 因此,理想情况下,该解决方案将利用这三种解决方案之一。 Everything I've come up with looks ugly, so I'm wondering if there is a simple way of doing something like this that I'm overlooking. 我提出的所有内容看起来都很丑陋,所以我想知道是否有一种简单的方法可以做我所忽略的事情。

What about this code, too ugly? 这段代码太丑了呢?

var data = JSON.parse(/*Your JSON */).data;
var bases = [];
var i;
var sites = data.sites.data;
var len = sites.length;

for (i = 0; i < len; i++) {
    var site = sites[i];
    var base = site.data.bases;

    bases.push(base.date);

    delete site.data.bases;
}

data.bases = {data : Array.prototype.concat.apply([], bases) };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM