按键分组对象

时间:2019-10-11 06:58:04

标签: c# json linq

//Car Model having Make,Model,Color 

public class Car
{
    public string Make { get; set; }
    public string Model { get; set; }
    public string Color { get; set; }
}

...

//List that holds the objects

List<Car> cars = new List<Car>();

cars.Add(new Car {Make = "Honda", Model = "Accord", Color = "blue"});
cars.Add(new Car {Make = "Dodge", Model = "Caravan", Color = "green"});
cars.Add(new Car {Make = "Ford", Model = "Crown Victoria", Color = "red"});
cars.Add(new Car {Make = "Honda", Model = "Civic", Color = "blue" });

我正在尝试

var carGroups = cars.GroupBy(c => c.Color);

List<ColorGroup> obj = new List<ColorGroup>();
foreach (var group in carGroups)
{
    ColorGroup cg = new ColorGroup();
    cg.Color = group.Key;

    foreach (var item in group)
    {
        cg.cars.Add(item);
    }

    obj.Add(cg);
}

我需要将json的{​​{1}}格式的Color格式输出为Key

array

3 个答案:

答案 0 :(得分:3)

(使用Json.NET

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;

(...)

var carGroups = cars
    .GroupBy(c => c.Color) // You can modify the key here: .GroupBy(c => "Color:"+c.Color)
    .ToDictionary(g => g.Key);

var json = JsonConvert.SerializeObject(carGroups, Formatting.Indented);

Console.WriteLine(json);

输出

{
  "blue": [
    {
      "Make": "Honda",
      "Model": "Accord",
      "Color": "blue"
    },
    {
      "Make": "Honda",
      "Model": "Civic",
      "Color": "blue"
    }
  ],
  "green": [
    {
      "Make": "Dodge",
      "Model": "Caravan",
      "Color": "green"
    }
  ],
  "red": [
    {
      "Make": "Ford",
      "Model": "Crown Victoria",
      "Color": "red"
    }
  ]
}

答案 1 :(得分:0)

您可以通过简单地遍历Dictionary<string, List<Car>>列表来创建类型为cars的字典,然后使用NewtonSoft JSON库(JSON.Net)将字典序列化为JSON字符串。

return JsonConvert.SerializeObject( yourDictionary );

答案 2 :(得分:0)

这是该问题的另一种解决方案。使用此代码,您可以使用复杂的组。我还添加了“忽略”属性以忽略color属性。

   static void Main(string[] args)
    {
      List<Car> cars = new List<Car>();
      cars.Add(new Car { Make = "Honda", Model = "Accord", Color = "blue" });
      cars.Add(new Car { Make = "Dodge", Model = "Caravan", Color = "green" });
      cars.Add(new Car { Make = "Ford", Model = "Crown Victoria", Color = "red" });
      cars.Add(new Car { Make = "Honda", Model = "Civic", Color = "blue" });
      var carGroups = cars.GroupBy(c => c.Color);
      List<ColorGroup> obj = new List<ColorGroup>();
      foreach (var group in carGroups)
      {
        ColorGroup cg = new ColorGroup();
        cg.Color = group.Key;
        foreach (var item in group)
        {
          cg.Cars.Add(item);
        }
        obj.Add(cg);
      }
      var s = JsonConvert.SerializeObject(obj);

    }
  }
  [Serializable()]
  public class Car {
    public string Make { get; set; }
    public string Model { get; set; }
    [XmlIgnore]
    [ScriptIgnore]
    public string Color { get; set; }
  }
  public class ColorGroup {
    public ColorGroup(){
      Cars = new List<Car>();
    }
    public string Color { get; set; }
    public List<Car> Cars { get; set; }
  }