0

巨大な JSON ファイルがあります。それは正しい構造ではなく、私はその構造を制御できません。これは、タイムスタンプに基づいてルーターのグループから送信されるデータです。特定のルーターのすべての情報を、タイムスタンプなどを配列として持つルーター オブジェクトに結合するオブジェクト構造を作成しようとしています。@quodlibetor は、私がこれに近づくための大きな助けになりました。また、json 内の name val ペアの名前から、オブジェクトのプロパティに自動的に名前を付けたいと考えています。

私の目的は、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"
            }
        ]
    }
}

元の 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"
      }
      ];  

コード:

    //  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>
4

1 に答える 1

2

必要なコードは次のとおりです。

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);
于 2012-04-12T15:25:02.027 に答える