开发者

in c#, how do i convert this data structure into Json

开发者 https://www.devze.com 2023-01-14 16:15 出处:网络
In C# i have an array of Calendar objects each Calendar object has an开发者_运维百科 array of CalendarEvent objects

In C# i have an array of Calendar objects

each Calendar object has an开发者_运维百科 array of CalendarEvent objects

each CalendarEvent object has a Date and Name property

i want to convert this to Json object but i want to change the data structure a bit so in the json object a calendar is an array of dates and an array of names (breakdown the CalendarEvent object)

i want something like this:

var myObject = return Json(new
                {
                    Calendars = new[]
                    {
                         Dates = new [] {myDateArray};
                         Names = new [] {myNameArray};
                    }
                }


IEnumerable<Calendar> calendars = ...

return Json(
    calendars.Select(calendar => new
    {
        Names = calendar.CalendarEvents.Select(e => e.Name),
        Dates = calendar.CalendarEvents.Select(e => e.Date)
    })
);


For .Net 3.5, you'd be looking for the DataContractJsonSerializer. You'll probably want to customise it to match what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消