gpt4 book ai didi

c# - 在 C# 中,如何将此数据结构转换为 Json

转载 作者:行者123 更新时间:2023-11-30 13:38:18 25 4
gpt4 key购买 nike

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

关于c# - 在 C# 中,如何将此数据结构转换为 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3642099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com