代码之家  ›  专栏  ›  技术社区  ›  leora Matt Lacey

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

  •  1
  • leora Matt Lacey  · 技术社区  · 14 年前

    在C#我有一个数组 日历 物体

    日历 对象具有 日历事件 物体

    每个 对象具有 姓名 财产

    我想把它转换成Json对象,但是我想稍微改变一下数据结构,所以在Json对象中,日历是一个日期数组和一个名称数组(分解CalendarEvent对象)

    我想要这样的东西:

    var myObject = return Json(new
                    {
                        Calendars = new[]
                        {
                             Dates = new [] {myDateArray};
                             Names = new [] {myNameArray};
                        }
                    }
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   Darin Dimitrov    14 年前
    IEnumerable<Calendar> calendars = ...
    
    return Json(
        calendars.Select(calendar => new
        {
            Names = calendar.CalendarEvents.Select(e => e.Name),
            Dates = calendar.CalendarEvents.Select(e => e.Date)
        })
    );
    
        2
  •  0
  •   cofiem    14 年前

    对于.NET3.5,您需要 DataContractJsonSerializer . 你可能会想 customise it 匹配你想要的。