如何转换所需的json格式的数据

时间:2014-07-12 18:39:49

标签: json angularjs

当我使用angular $ http服务通过API调用获取数据时,我的数据看起来像这样,

 {"Modules":
     [{"ModuleId":"4b601ab6-7086-4094-a9dc-392a69cfbaa3","ModuleName":"Membership"},
      {"ModuleId":"82372172-6d82-453a-9752-39d31b795ea5","ModuleName":"Loan"},
      {"ModuleId":"3b4f4b77-c6d0-4eb5-bd6b-7aad74190783","ModuleName":"ContactUs"},
      {"ModuleId":"0ef66293-1356-46f7-b7eb-9394b9eb2e34","ModuleName":"About Us"}
      {"ModuleId":"6a2fb938-bfda-4799-8b96-a9f6c2886e29","ModuleName":"FeedBack"},  
      {"ModuleId":"a0a03589-f501-4e72-bad6-bf4743e2a4d1","ModuleName":"Fee",},
      {"ModuleId":"9ed698a6-859e-40c6-a063-e6a860b69ea5","ModuleName":"Home"}],
 "SubModules":[{"SubModuleId":"3ac796d0-4eab-4ce8-bb17-3a1a3e6a9431","ModuleId":"4b601ab6-7086-4094-a9dc-392a69cfbaa3","SubModuleName":"Get Membership",},
              {"SubModuleId":"fe4d061c-6aa3-4dbd-b5f8-460a85acbda2","ModuleId":"82372172-6d82-453a-9752-39d31b795ea5","SubModuleName":"Structure",},
              {"SubModuleId":"614107e2-b9c0-4cc3-a947-512768a910fb","ModuleId":"4b601ab6-7086-4094-a9dc-392a69cfbaa3","SubModuleName":"Our Members",},
              {"SubModuleId":"15b02cd2-46b6-481d-9480-d321c9644379","ModuleId":"82372172-6d82-453a-9752-39d31b795ea5","SubModuleName":"form"},
              {"SubModuleId":"44ea642a-99d0-4f1a-bdeb-e033ecbd3e55","ModuleId":"4b601ab6-7086-4094-a9dc-392a69cfbaa3","SubModuleName":"Inquiry"}]} 

如果您观察到上述数据,则“模块”和“子模块”通过“ModuleId”连接。

我想将以上数据转换为以下格式,

   ModuleName:
              Membership :
                           Get Membership
                           Our Members
                           Inquiry
                    Loan :
                           structure
                           form
               Contact Us:
               About Us  :

我想让json / obj与此处显示的相似或相关。我知道这根本不完美。但我对Json的处理太多了所以我甚至不知道怎么写这个。 任何帮助将不胜感激。

稍后,我想使用ng-repeat使用此数据创建动态菜单。

1 个答案:

答案 0 :(得分:0)

嗨这有帮助吗?

var testData = {
    "Modules": [
        {
            "ModuleId": "4b601ab6-7086-4094-a9dc-392a69cfbaa3",
            "ModuleName": "Membership"
        },
        {
            "ModuleId": "82372172-6d82-453a-9752-39d31b795ea5",
            "ModuleName": "Loan"
        },
        {
            "ModuleId": "3b4f4b77-c6d0-4eb5-bd6b-7aad74190783",
            "ModuleName": "ContactUs"
        },
        {
            "ModuleId": "0ef66293-1356-46f7-b7eb-9394b9eb2e34",
            "ModuleName": "About Us"
        },
        {
            "ModuleId": "6a2fb938-bfda-4799-8b96-a9f6c2886e29",
            "ModuleName": "FeedBack"
        },
        {
            "ModuleId": "a0a03589-f501-4e72-bad6-bf4743e2a4d1",
            "ModuleName": "Fee",

        },
        {
            "ModuleId": "9ed698a6-859e-40c6-a063-e6a860b69ea5",
            "ModuleName": "Home"
        }
    ],
    "SubModules": [
        {
            "SubModuleId": "3ac796d0-4eab-4ce8-bb17-3a1a3e6a9431",
            "ModuleId": "4b601ab6-7086-4094-a9dc-392a69cfbaa3",
            "SubModuleName": "Get Membership",

        },
        {
            "SubModuleId": "fe4d061c-6aa3-4dbd-b5f8-460a85acbda2",
            "ModuleId": "82372172-6d82-453a-9752-39d31b795ea5",
            "SubModuleName": "Structure",

        },
        {
            "SubModuleId": "614107e2-b9c0-4cc3-a947-512768a910fb",
            "ModuleId": "4b601ab6-7086-4094-a9dc-392a69cfbaa3",
            "SubModuleName": "Our Members",

        },
        {
            "SubModuleId": "15b02cd2-46b6-481d-9480-d321c9644379",
            "ModuleId": "82372172-6d82-453a-9752-39d31b795ea5",
            "SubModuleName": "form"
        },
        {
            "SubModuleId": "44ea642a-99d0-4f1a-bdeb-e033ecbd3e55",
            "ModuleId": "4b601ab6-7086-4094-a9dc-392a69cfbaa3",
            "SubModuleName": "Inquiry"
        }
    ]
};

实际代码:

var structuredData = {};

//Iterate over module data
for (var m = 0; m < testData.Modules.length; m++){
    //create key in object for each module
    structuredData[testData.Modules[m].ModuleName] = {};

    //Iterate over sub module
    for (var s = 0; s < testData.SubModules.length; s++){

        //Is sub module belonging to module
        if(testData.SubModules[s].ModuleId == testData.Modules[m].ModuleId){

            //create key in module for sub module
            structuredData[testData.Modules[m].ModuleName][testData.SubModules[s].SubModuleName] = {};
        }
    }
}

console.log(structuredData);

这应该创建一个具有嵌套结构的对象,如图中所示。

http://jsfiddle.net/FQtLW/1/