Reputation: 9984
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
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
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