简体   繁体   English

将json自动操作到具有不同结构的对象中

[英]Manipulate json into an object with a different structure automatically

I have a huge JSON file. 我有一个巨大的JSON文件。 It's not in the correct structure and I have no control over the structure. 它没有正确的结构,我无法控制结构。 It is data coming from a group of routers based on timestamp. 它是来自一组基于时间戳的路由器的数据。 I'm trying to create an object structure that combines all the info for a given router into a router object with timestamp etc. as arrays. 我正在尝试创建一个对象结构,它将给定路由器的所有信息组合成一个带时间戳等的路由器对象作为数组。 @quodlibetor was a big help getting me close to this. @quodlibetor是一个很大的帮助让我接近这一点。 I also want to automatically name the properties of the object from the name of the name val pairs in the json. 我还想从json中名称val对的名称自动命名对象的属性。

My aim is to autoname the properties from the names in the json file and to restructure it into an object that looks like this (I'm open to suggestions on the structure this just seemed the most organized way to do it). 我的目标是从json文件中的名称自动命名属性,并将其重组为一个看起来像这样的对象(我对结构的建议是开放的,这似乎是最有组织的方式)。

This is the structure I want: 这是我想要的结构:

    {
        "Router": [
            {
                "routerName": "RouterID1",
                "TimeStamp": [
                    "2012/01/01 06:00:00",
                    "2013/01/01 06:00:00",
                    "2014/01/01 06:00:00"
                ],
                "OutputBITS": [
                    "23235",
                    "29903",
                    "22103"
                ],
                "InputBITS": [
                    "23235",
                    "29903",
                    "22103"
                ]
            }
        ]
    }

Although trying to create that structure I'm getting close but no deal: 虽然尝试创建该结构,但我已经接近但没有交易:

{
    "RouterID1": {
        "timeStamp": [
            {
                "timeStamp": "2012/01/01 06:00:00"
            },
            {
                "timeStamp": "2013/01/01 06:00:00"
            },
            {
                "timeStamp": "2014/01/01 06:00:00"
            }
        ],
        "OutputBITS": [
            {
                "OutputBITS": "23235"
            },
            {
                "OutputBITS": "23235"
            },
            {
                "OutputBITS": "23235"
            }
        ],
        "InputBITS": [
            {
                "InputBITS": "29903"
            },
            {
                "InputBITS": "29903"
            },
            {
                "InputBITS": "29903"
            }
        ]
    }
}

ORIGINAL JSON: 原始JSON:

json = [
         {
              "Router": "RouterID1",
              "TimeStamp": "2012/01/01 06:00:00",
              "OutputBITS": "23235",
              "InputBITS":  "29903"
         },   
          {
              "Router": "RouterID1",
              "TimeStamp": "2013/01/01 06:00:00",
              "OutputBITS": "23235",
              "InputBITS":  "29903"
         },   
          {
              "Router": "RouterID1",
              "TimeStamp": "2014/01/01 06:00:00",
              "OutputBITS": "23235",
              "InputBITS":  "29903"
         },
         {
              "Router": "RouterID3",
              "TimeStamp": "2012/01/01 06:05:00",
              "OutputBITS": "21235",
              "InputBITS":  "22103"
         },
         {
             "Router": "RouterID3",
             "TimeStamp": "2012/01/01 06:05:00",
             "OutputBITS": "21235",
             "InputBITS":  "22103"
        },
        {
            "Router": "RouterID4",
            "TimeStamp": "2012/01/01 06:05:00",
            "OutputBITS": "21235",
            "InputBITS":  "22103"
       },
       {
           "Router": "RouterID4",
           "TimeStamp": "2012/01/01 06:05:00",
           "OutputBITS": "21235",
           "InputBITS":  "22103"
      }
      ];  

CODE: 码:

    //  Create routers object    
    var routers = {};


       for (var i=0;i<json.length;i++){
        var router_name = json[i].Router;
        router_name = (router_name.replace(/-/g, ""));  //take hyphen out or router name

        if (!routers[router_name]){
            // add properties to the router object thanks to @@quodlibetor

            // instead of using timeStamp is something like json[i] or json.[name] or some
            // way to reference each json property and not have to type it in?

            routers[router_name] = { timeStamp : [], OutputBITS : [], InputBITS : [] };
        }

                routers[router_name].timeStamp.push({
                    timeStamp : json[i].TimeStamp
                }); 
                 routers[router_name].OutputBITS.push({
                     OutputBITS : json[i].OutputBITS
                }); 
                routers[router_name].InputBITS.push({
                    InputBITS : json[i].InputBITS
                });  
    }; 

    console.log(routers);
    });
     </script>

Here is the code you need: 这是您需要的代码:

var router = {router: []}, i, j, k, l, inArray, routerName, thisRouter;

for(i=0,j=json.length;i<j;++i) {
    inArray = false;

    routerName = json[i].Router;

    for(k=0,l=router.router.length;k<l;++k) {
        if(router.router[k].name === routerName) {
            inArray = true; --k; l = k;
        }
    }

    if(inArray === true) {
        router.router[k].TimeStamp.push(json[i].TimeStamp);
        router.router[k].OutputBITS.push(json[i].OutputBITS);
        router.router[k].InputBITS.push(json[i].InputBITS);
    } else {
        thisRouter = {name: routerName, TimeStamp: [json[i].TimeStamp], OutputBITS: [json[i].OutputBITS], InputBITS: [json[i].InputBITS]};

        router.router.push(thisRouter);
    }
}

console.log(router);

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

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