Javascript:重组JSON

时间:2016-11-22 04:49:15

标签: javascript json

我有来自服务器的JSON响应,如 -

[
  {
    "ASSET_CLIENT_CATALOG_ID": "3EB06E303C9E6F70E053421F080A5AF3",
    "ParentClientId": "3EB06E303A596F70E053421F080A5AF3",
    "CATEGORY": "Power Cutter",
    "ASSET_CATALOG_GROUP_TYPE": "PRODUCT",
    "ASSET_CLIENT_CATALOG_NAME": "POWER CUTTER",
    "ParentCategoryName": "Small Household Appliances",
    "ASSET_CATALOG_LEVEL": 2
  },
  {
    "ASSET_CLIENT_CATALOG_ID": "3EB06E303C9F6F70E053421F080A5AF3",
    "ParentClientId": "3EB06E303A596F70E053421F080A5AF3",
    "CATEGORY": "Rotary Tool Kit",
    "ASSET_CATALOG_GROUP_TYPE": "PRODUCT",
    "ASSET_CLIENT_CATALOG_NAME": "ROTARY TOOL KIT",
    "ParentCategoryName": "Small Household Appliances",
    "ASSET_CATALOG_LEVEL": 2
  },
  {
    "ASSET_CLIENT_CATALOG_ID": "3EB06E303DFF6F70E053421F080A5AF3",
    "ParentClientId": "3EB06E303A566F70E053421F080A5AF3",
    "CATEGORY": "Enlarger",
    "ASSET_CATALOG_GROUP_TYPE": "PRODUCT",
    "ASSET_CLIENT_CATALOG_NAME": "ENLARGER",
    "ParentCategoryName": "Cameras & Photography",
    "ASSET_CATALOG_LEVEL": 2
  },
  {
    "ASSET_CLIENT_CATALOG_ID": "3EB06E303E006F70E053421F080A5AF3",
    "ParentClientId": "3EB06E303A566F70E053421F080A5AF3",
    "CATEGORY": "Flash Transceiver",
    "ASSET_CATALOG_GROUP_TYPE": "PRODUCT",
    "ASSET_CLIENT_CATALOG_NAME": "FLASH TRANSCEIVER",
    "ParentCategoryName": "Cameras & Photography",
    "ASSET_CATALOG_LEVEL": 2
  },
]

我想重组数据如下 -

[
    {
        "ParentCategoryName": "Cameras & Photography",
        "ParentClientId": "3EB06E303A596F70E053421F080A5AF3",
        "Category":
        [
            {
                "ASSET_CLIENT_CATALOG_NAME": "FLASH TRANSCEIVER",
                "ASSET_CLIENT_CATALOG_ID": "3EB06E303E006F70E053421F080A5AF3"                  
            },
            {
                "ASSET_CLIENT_CATALOG_NAME": "ENLARGER",
                "ASSET_CLIENT_CATALOG_ID": "3EB06E303DFF6F70E053421F080A5AF3"                  
            }
        ]
    },
    {
        "ParentCategoryName": "Small Household Appliances",
        "ParentClientId": "3EB06E303A596F70E053421F080A5AF3",
        "Category": 
        [
            {
                "ASSET_CLIENT_CATALOG_NAME": "ROTARY TOOL KIT",
                "ASSET_CLIENT_CATALOG_ID": "3EB06E303C9F6F70E053421F080A5AF3"                  
            },
            {
                "ASSET_CLIENT_CATALOG_NAME": "POWER CUTTER",
                "ASSET_CLIENT_CATALOG_ID": "3EB06E303C9E6F70E053421F080A5AF3"                  
            }
        ]
    }
]

有没有人可以帮我用Javascript代码来重构JSON。 我无法使结构正确。

由于

1 个答案:

答案 0 :(得分:0)

如果您要存储重组的资产列表或保存副本,请使用 JSON.stringify 获取干净的内容并写入文件或任何位置你想要。

使用方法:

var restructured = listReCompile( [sourceObject] );

返回的活动对象也将包含属性&提供“ParentClientId”代码的直接访问链接,以便您方便地直接访问该类别中的可用资产列表。

的问候。

function listReCompile(x) {

  var i = 0, m = [];
  while ( x[i] ) { var o = {}, so = {};

    if ( x[i]["ParentClientId"] in m ) {

      so["ASSET_CLIENT_CATALOG_NAME"] = x[i]["ASSET_CLIENT_CATALOG_NAME"];
      so["ASSET_CLIENT_CATALOG_ID"] = x[i]["ASSET_CLIENT_CATALOG_ID"];
      m[ x[i]["ParentClientId"] ].push( so );

    }
    else
    {
      o["ParentCategoryName"] = x[i]["ParentCategoryName"];
      o["ParentClientId"] = x[i]["ParentClientId"];
      o["Category"] = [];

      so["ASSET_CLIENT_CATALOG_NAME"] = x[i]["ASSET_CLIENT_CATALOG_NAME"];
      so["ASSET_CLIENT_CATALOG_ID"] = x[i]["ASSET_CLIENT_CATALOG_ID"];

      o.Category.push( so );

      m[ x[i]["ParentClientId"] ] = o.Category;
      m.push( o );
    }
    i++;
  }
  return m;
};

var source = [{
  "ParentCategoryName": "Cameras & Photography",
  "ParentClientId": "3EB06E303A566F70E053421F080A5AF3",
  "CATEGORY": "Flash Transceiver",
  "ASSET_CLIENT_CATALOG_NAME": "FLASH TRANSCEIVER",
  "ASSET_CLIENT_CATALOG_ID": "3EB06E303E006F70E053421F080A5AF3",
  "ASSET_CATALOG_GROUP_TYPE": "PRODUCT",

  "ASSET_CATALOG_LEVEL": 2
}, {
  "ParentCategoryName": "Cameras & Photography",
  "ParentClientId": "3EB06E303A566F70E053421F080A5AF3",
  "CATEGORY": "Enlarger",
  "ASSET_CLIENT_CATALOG_NAME": "ENLARGER",
  "ASSET_CLIENT_CATALOG_ID": "3EB06E303DFF6F70E053421F080A5AF3",
}, {
  "ASSET_CLIENT_CATALOG_ID": "3EB06E303C9E6F70E053421F080A5AF3",
  "ParentClientId": "3EB06E303A596F70E053421F080A5AF3",
  "CATEGORY": "Power Cutter",
  "ASSET_CATALOG_GROUP_TYPE": "PRODUCT",
  "ASSET_CLIENT_CATALOG_NAME": "POWER CUTTER",
  "ParentCategoryName": "Small Household Appliances",
  "ASSET_CATALOG_LEVEL": 2
}, {
  "ASSET_CLIENT_CATALOG_ID": "3EB06E303C9F6F70E053421F080A5AF3",
  "ParentClientId": "3EB06E303A596F70E053421F080A5AF3",
  "CATEGORY": "Rotary Tool Kit",
  "ASSET_CATALOG_GROUP_TYPE": "PRODUCT",
  "ASSET_CLIENT_CATALOG_NAME": "ROTARY TOOL KIT",
  "ParentCategoryName": "Small Household Appliances",
  "ASSET_CATALOG_LEVEL": 2
} ];

var restructured = listReCompile( source );

console.log( restructured );