0

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

MGId MGName FGId FGName itemId itemName
6272440:Beverage:6272963:Coke:6274904:pepsi@@@

我使用 jsonObj[MGId][FGId][MIId],我希望这会在 jsonObj[MGId][FGId] 下创建单独的 [MIId] .. 显然我遗漏了一些东西,或者它不能以这种方式工作。谁能告诉我缺少什么或欢迎任何替代方式。谢谢!!

/*
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"
            }
        }
    }
}
4

1 回答 1

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

    };
于 2013-07-18T06:36:14.650 回答