json将动态子项添加到动态键

时间:2013-07-18 06:18:39

标签: json dynamic key

我正在尝试格式化一堆数据,我能够以下面的格式检索。 (我还把一个可读的版本注释掉了) 我希望以OUTPUT(见底部)显示的方式将每一行放在 json对象 中。但它只保留最后一项的细节,而我希望每个项目都保持在正确的MGId和FGId值之下。

  

MGId MGName FGId FGName itemId itemName
  6272440:饮料:6272963:焦炭:6274904:百事可乐@@@

我使用jsonObj [MGId] [FGId] [MIId],我期待这个创建单独的[MIId] jsonObj [MGId] [FGId] ..显然我错过了一些东西,或者它不能以这种方式工作。 任何人都可以告诉我缺少什么或任何替代方式欢迎。谢谢!

/*
DATA READABLE FORMAT
6272440:Beverage:6272963:Coke:6274904:pepsi@@@
6272440:Beverage:6272963:coke:6274494:coco cola 1 lt@@@
6272440:Beverage:6272951:Milk:6274300:Skim 1 lt@@@
6272440:Beverage:6272951:Milk:6274130:Full Cream@@@
6272437:Food:6272919:Breakfast Menu:6274947:Bacon and Egg@@@
6272437:Food:6272915:Lunch Menu:6274766:Chicken Burger@@@
6272437:Food:6272915:Lunch Menu:6274922:Vegi Sandwich@@@
6272437:Food:6272915:Lunch Menu:6274900:Garlic Bread";
*/

var sampleData = "6272440:Beverage:6272963:Coke:6274904:pepsi@@@6272440:Beverage:6272963:coke:6274494:coco cola 1 lt@@@6272440:Beverage:6272951:Milk:6274300:Skim 1 lt@@@6272440:Beverage:6272951:Milk:6274130:Full Cream@@@6272437:Food:6272919:Breakfast Menu:6274947:Bacon and Egg@@@6272437:Food:6272915:Lunch Menu:6274766:Chicken Burger@@@6272437:Food:6272915:Lunch Menu:6274922:Vegi Sandwich@@@6272437:Food:6272915:Lunch Menu:6274900:Garlic Bread";
  var elements = sampleData.split("@@@"); 
  var jsonObj = {};
  var menuItemsArr = [];
  var previousMGId="";
  var previousFGId="";

  for(var i=0; i<elements.length; i++) {

     var nextMenuPack = elements[i];

     var nextMenuPackInnerHtml = nextMenuPack; //nextMenuPack.innerHTML;
     var nextMenuPackArr       = nextMenuPackInnerHtml.split(":"); 
     var MGId   = nextMenuPackArr[0];
     var MGName = nextMenuPackArr[1];
     var FGId   = nextMenuPackArr[2];
     var FGName = nextMenuPackArr[3];
     var MIId   = nextMenuPackArr[4];
     var MIName = nextMenuPackArr[5];
     console.log(MGName +"-"+FGName+"-"+MIName);


    jsonObj[MGId] = {}  ;
    jsonObj[MGId][FGId] = {};       
    //var jsonObj = {};
    jsonObj[MGId][FGId][MIId] = {       
                    "MGName" : MGName, 
                    "MGId": MIId,
                    "FGName": FGName,
                    "FGId": FGId,
                    "MIName" : MIName, 
                    "MIId": MIId

        };

}    


 document.write('<pre>'+JSON.stringify(jsonObj, null, "\t")+'</pre>');

输出

{
    "6272440": { //major group id
        "6272951": { //family group id
            "6274130": { // menuitem id - JUST LAST ONE, OTHERS OVERWRITTEN!
                "MGName": "Beverage",
                "MGId": "6274130",
                "FGName": "Milk",
                "FGId": "6272951",
                "MIName": "Full Cream",
                "MIId": "6274130"
            }
        }
    },
    "6272437": {
        "6272915": {
            "6274900": {// menuitem id - JUST LAST ONE, OTHERS OVERWRITTEN!
                "MGName": "Food",
                "MGId": "6274900",
                "FGName": "Lunch Menu",
                "FGId": "6272915",
                "MIName": "Garlic Bread",
                "MIId": "6274900"
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

只有初始化jsonObj[MGId]jsonObj[MGId][FGId](如果它们尚未存在)。

jsonObj[MGId] = jsonObj[MGId] || {}  ;
jsonObj[MGId][FGId] = jsonObj[MGId][FGId] || {};       
//var jsonObj = {};
jsonObj[MGId][FGId][MIId] = {       
                "MGName" : MGName, 
                "MGId": MIId,
                "FGName": FGName,
                "FGId": FGId,
                "MIName" : MIName, 
                "MIId": MIId

    };