chugh97
chugh97

Reputation: 9984

Creating the following json object from a jquery array of menu items and submenu items

I have a context menu which has menu items and sub menu items. A menu can have zero or more sub menu items.I need to map my menu object to a json object as follows. How can I do it?

            var commonMenuItems = [
              {
                  Menu: "Print",
                  Caption: "Print"


                  },
                  SubMenuItems: [
                    {
                        SubMenuItemName: "PrintSelected",
                        SubMenuItemDisplayName: "Print Selected",

                    },
                    {
                        SubMenuItemName: "PrintAll",
                        SubMenuItemDisplayName: "Print All",

                    }
                ]
              }

        ];

I want to map commonMenuItems array to items json object items programatically.

  items: {
                "Print": {"name": "Print"
                  "items":{
                       "PrintSelected": {"name": "Print Selected"},
                       "PrintAll": {"name": "Print All"},
                  }
}

Upvotes: 1

Views: 253

Answers (2)

Vivek Muthal
Vivek Muthal

Reputation: 1015

Assuming that you are returning commonMenuItems at client then converting it to javascript array var commonMenuItems = datagotfromserver; then to convert it to json you can simply use var items = JSON.stringify(commonMenuItems);.

Upvotes: 0

Bhushan Firake
Bhushan Firake

Reputation: 9458

You can use JsonSerializer. There are many available.JSON.NET and ServiceStack.NET serializers are better options but I recommend you ServiceStack Serializer from NuGet Package.It is the fastest serializer on the planet.

Upvotes: 1

Related Questions