将List转换为Dictionary时获取重复的JSON项



我正在创建一个JSON文件,方法是从对象列表中读取一些数据,将其转换为嵌套字典并序列化结果。所需的JSON格式如下:

{
  "Employee1": {
    "YYY": {
      "StartRange": 11,
      "EndRange": 22
    }
  },
  "Employee2": {
    "XXX": {
      "StartRange": 24,
      "EndRange": 56
    }
  }
}

但我得到的是JSON输出:

{
  "Employee1": {
    "YYY": {
      "StartRange": 11,
      "EndRange": 22
    },
    "XXX": {
      "StartRange": 11,
      "EndRange": 22
    }
  },
  "Employee2": {
    "YYY": {
      "StartRange": 24,
      "EndRange": 56
    },
    "XXX": {
      "StartRange": 24,
      "EndRange": 56
    }
  }
}

这是我正在使用的代码:

public class LabelData
{
    public int StartRange { get; set; }
    public int EndRange { get; set; }
}
public class ConfigInfo
{
    public string ParentGroup;
    public string Label;
    public int ID;
    public int StartRange;
    public int EndRange;
}
public Dictionary<string, Dictionary<string, LabelData>> GetData(List<ConfigInfo> configList)
{
    var labelData = new Dictionary<string, Dictionary<string, LabelData>>();
    foreach (var listItem in configList)
    {
        labelData[listItem.ParentGroup] = configList.Distinct().ToDictionary(x => x.Label.ToString(), row => new LabelData()
        {
         StartRange = Convert.ToInt32(listItem.StartRange.ToString()),
         EndRange = Convert.ToInt32(listItem.EndRange.ToString())
        });
    }
    return labelData;
}
protected void createFile()
{
    List<ConfigInfo> configInfoList = new List<ConfigInfo>();
    ConfigInfo configInfo = new ConfigInfo();
    configInfo.ParentGroup = "Employee1";
    configInfo.StartRange = 11;
    configInfo.EndRange = 22;
    configInfo.Label = "YYY";
    configInfoList.Add(configInfo);
    configInfo = new ConfigInfo();
    configInfo.ParentGroup = "Employee2";
    configInfo.StartRange = 24;
    configInfo.EndRange = 56;
    configInfo.Label = "XXX";
    configInfoList.Add(configInfo);
    if (!File.Exists(fileName))
    {
        FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs);
        JsonTextWriter writer = new JsonTextWriter(sw);
        Dictionary<string, Dictionary<string, LabelData>> data = GetData(configInfoList);
        string json = JsonConvert.SerializeObject(data,Formatting.Indented);
        sw.Write(json);
        sw.Close();
    }
}

我认为问题可能与两个类中都有StartRange和EndRange有关,但我不知道如何解决。我做错了什么?

问题在于GetData方法中的以下代码:

var labelData = new Dictionary<string, Dictionary<string, LabelData>>();
foreach (var listItem in configList)
{
    labelData[listItem.ParentGroup] = 
        configList.Distinct().ToDictionary(x => x.Label.ToString(), row => new LabelData()
    {
        StartRange = Convert.ToInt32(listItem.StartRange.ToString()),
        EndRange = Convert.ToInt32(listItem.EndRange.ToString())
    });
}

对于列表中的每个ConfigInfo项目,您将向结果添加一个Dictionary,该结果包含列表中每个项目的Label以及当前项目的StartRangeEndRange

我认为您真正想要的是按ParentGroup对项目进行分组,然后为每个组创建仅包含该组中项目的字典。将上面的代码替换为以下代码,您应该会得到您期望的输出:

var labelData = configList
    .GroupBy(c => c.ParentGroup)
    .ToDictionary(
        g => g.Key,
        g => g.ToDictionary(
            c => c.Label,
            c => new LabelData
            {
                StartRange = c.StartRange,
                EndRange = c.EndRange
            }
        )
    );

Fiddle:https://dotnetfiddle.net/nzkj0h

顺便说一句,我认为我建议将GetData方法重命名为TransformData,因为它实际上就是这样做的。

相关内容

  • 没有找到相关文章

最新更新