簡體   English   中英

在C#中,我如何將此數據結構轉換為Json

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

在C#中,我有一個Calendar對象數組

每個Calendar對象都有一個CalendarEvent對象數組

每個CalendarEvent對象都有一個DateName屬性

我想將其轉換為Json對象,但我想稍微更改數據結構,因此在json對象中,日歷是日期數組和名稱數組(分解CalendarEvent對象)

我想要這樣的事情:

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)
    })
);

對於.Net 3.5,您將尋找DataContractJsonSerializer 您可能需要自定義它以匹配您想要的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM