将Dynamic C#Object序列化为JSON

时间:2014-12-30 04:17:41

标签: c# asp.net-mvc razor

我有一个ASP.NET MVC应用程序。这个应用程序包括C#,Razor和JSON.NET。我有一个C#对象,如下所示:

public class MyBlueprint
{
  public string Name { get; set; }

  public string Approach { get; set; }

  public object Value { get; set; }
}

目前,我有一个MyBlueprint对象列表,如下所示:

List<MyBlueprint> x = new List<MyBlueprint>();
{
  new MyBlueprint { Name="Bill", Approach="straight", Value=1 },
  new MyBlueprint { Name="Bill", Approach="straight", Value=7 },
  new MyBlueprint { Name="Bill", Approach="straight", Value=10 },
  new MyBlueprint { Name="John", Approach="straight", Value=120 },
  new MyBlueprint { Name="John", Approach="straight", Value=105 },
  new MyBlueprint { Name="John", Approach="straight", Value=150 },
  new MyBlueprint { Name="John", Approach="straight", Value=100 },
  new MyBlueprint { Name="Jill", Approach="curved", Value=1 },
  new MyBlueprint { Name="Jill", Approach="curved", Value=2 },
  new MyBlueprint { Name="Jill", Approach="curved", Value=5 },
  new MyBlueprint { Name="Jill", Approach="curved", Value=8 }
};

我需要将MyBlueprint个对象的集合展平为预定义的JSON结构。最终目标是在我的视图中使用JSON,如下所示:

var results = [
  { "name": "Bill", "approach":"straight", options: { begin: 1, end: 10 } },
  { "name": "John", "approach":"straight", options: { begin: 100, end: 150 } },
  { "name": "Jill", "approach":"curved", options: { chosen: [1, 2, 5, 8] } }
];

options参数根据approach的值具有不同的属性。上面的JSON显示了两种不同的结构:curvedstraight。我会有更多。出于这个原因,我试图在不创建大量模型类的情况下找出动态方法。目前,我有以下内容:

var names = new[] { "Bill", "John", "Jill" };
foreach (var name in names)
{
  var blueprints = x.Where(y => y.Name == name).Select(z => z.Value.ToString());    
}

我知道如何获取chosen集的curved值列表。我知道如何获取begin集的endstraight值。但是,一旦我拥有它们,我不知道如何处理它们。我试图找出如何创建一个动态C#对象,我可以将其序列化为JSON并将其置于视图中。我错过了什么?我觉得有点卡住了。

2 个答案:

答案 0 :(得分:1)

如果您愿意,可以使用匿名类型构建您正在寻找的数据结构。假设你的&#34;蓝图&#34;存储在myBluePrints

var toBeSerialized = myBlueprints
    .GroupBy(bp => new {bp.Name, bp.Approach})
    .Select(bpg => new
    {
        name = bpg.Key.Name,
        approach = bpg.Key.Approach,
        options = new
        {
            begin = bpg.Min(bp => bp.Value),
            end = bpg.Max(bp => bp.Value)
        }
    });

那就是说,我认为构建它的最简单方法是编写一个对应于options的类,以及一个对应于数组中顶级蓝图对象的类。换句话说,&#34吨模型类&#34; (两个,特别是)您试图避免的可能是解决此问题的最简单方法。

答案 1 :(得分:1)

我建议您必须做以下事情。

创建课程。

public class Options
{
    public Options()
    {
    }

    public Options(List<int> d,string approach)
    {
       if (approach == "straight")
       {
          begin = d.Min();
          end = d.Max();
       }
       else if (approach == "curved")
       {
           choosen = d.Select(cc => cc).ToArray();
       }
    }

    public int? begin { get; set; }
    public int? end { get; set; }
    public int[] choosen { get; set; }
}

和你的linq查询

 var result = (from item in x
                          group item by new { item.Name, item.Approach } into g
                          select new
                          {
                              name = g.Key.Name,
                              approach = g.Key.Approach,
                              options = new Options(g.Select(cc => Convert.ToInt32(cc.Value)).ToList(), g.Key.Approach)
                          }).ToList();

            string value = Newtonsoft.Json.JsonConvert.SerializeObject(result, new Newtonsoft.Json.JsonSerializerSettings() { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore });