将json自动处理为具有不同结构的对象

将json自动处理为具有不同结构的对象

问题描述:

我有一个巨大的JSON文件。它不在正确的结构中,我无法控制结构。它是基于时间戳的来自一组路由器的数据。我试图创建一个对象结构,它将给定路由器的所有信息与时间戳等路由器对象组合成一个数组。 @quodlibetor让我接近这个很有帮助。我也想自动从json中名称val对的名称中命名对象的属性。将json自动处理为具有不同结构的对象

我的目的是自动调用json文件中名称的属性,并将其重构为一个看起来像这样的对象(我对这个结构提出建议,这似乎是最有组织的方式) 。

这是我想要的结构:

{ 
     "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" 
       ] 
      } 
     ] 
    } 

虽然他们试图创建一个结构,我越来越接近,但没有成交:

{ 
    "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 = [ 
     { 
       "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> 
+0

请告诉我你在一开始的JSON? – 2012-04-12 15:23:46

+0

我编辑了我的答案 – 2012-04-12 15:31:29

+0

[{是什么在开始。这一切都在那里。我会在下面检查你的答案。 – rd42 2012-04-12 16:52:27

这里是你需要的代码:

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); 
+0

感谢您的回答。这可能是因为你没有原始的JSON(它现在在那里),但它是抛出和错误:routers ['name'] = Router; 未捕获ReferenceError:路由器未定义 – rd42 2012-04-12 17:05:41

+0

对不起,我离开电脑,我现在编程它。 – 2012-04-12 17:35:30

+0

@ rd42现在它应该做你想做的事情。 – 2012-04-12 17:37:40