对列表中的值进行分组,并使用 C# 将输出转换为 JSON

本文关键字:输出 JSON 转换 列表 | 更新日期: 2023-09-27 18:36:13

我有一个对象列表。它有 4 个属性。 Month, Category, 2015 & 2014

 Month  Category    2015    2014
    Jan A   10  100
    Jan B   20  200
    Jan C   30  300
    Jan D   40  400
    Feb B   50  500
    Feb C   60  600
    Feb D   70  700
    Mar A   80  800
    Mar I   90  900
    Mar J   100 1000
我想

使用月份对值进行分组,我想像这样生成 JSON 输出。我通过在类别名称中前缀curpre关键字来创建JSON对象中的属性,它们所代表的值来自2014 and 2015

[{  
    name: "jan",
    curA: 10,
    preA: 100,
    curB: 20,
    preB: 200,
    curC: 30,
    preC: 400,
    curD: 40,
    preD: 400
}, {    
    name: "feb",
    curB: 50,
    preB: 500,
    curC: 60,
    preC: 600,
    curD: 70,
    preD: 700
}, {    
    name: "mat",
    curA: 80,
    preA: 800,
    curI: 90,
    preI: 900,
    curJ: 100,
    preJ: 1000
}]  

我可以使用 JSON.NET 将 C# 对象序列化为 JSON,但我正在努力创建一个可以转换为所需格式的类。

对列表中的值进行分组,并使用 C# 将输出转换为 JSON

您可以使用字典

数组。

var list = new List<Dictionary<string, int>> ();

您必须弄清楚如何根据数据填充字典,但您需要这样做的月份/细分点:

var data = new Dictionary<string, int>
{
     { "name", 10 },
     { "curA", 100 },
     { "preA", 20 } // And so on...
};
list.Add(data);

然后,您可以以类似的方式将列表转换为 json:

string json = JsonConvert.SerializeObject(points, Formatting.Indented);

希望这有帮助!

编辑:最初的问题是寻求帮助将他的源列表转换为JSON字符串中的所需列表。

在不知道源列表(对象、数据行等)的格式的情况下,我假设你只有一个对象列表。我的对象如下所示:

public class ListRow {
    public string Month { get; set; }
    public string Category { get; set; }
    public string _2015 { get; set; }
    public string _2014 { get; set; }
}

我还假设您有一个名为 list 的变量,其中包含这些对象的列表。未填充的定义如下所示:

var list = new List<ListRow> ();

我很快将这些未经测试的代码放在一起,作为如何将源列表转换为新格式的指南。

    var convertedList = new List<Dictionary<string, string>> ();
    var groupedList = list.GroupBy (_ => _.Month);
    foreach (var item in groupedList) {
        var data = new Dictionary<string, string> ();
        data.Add ("name", item.Key);
        foreach (var value in item) {
            data.Add (string.Format ("cur{0}", value.Category), value._2015);
            data.Add (string.Format ("pre{0}", value.Category), value._2014);
        }
        convertedList.Add (data);
    }

然后,您需要序列化convertedList变量。

希望这能让您更接近您的解决方案。

这是我

的实现:

    using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
    public class Transactions
    {
        public string Month { get; set; }
        public string Cath { get; set; }
        public string Year { get; set; }
        public int Unit { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var transactionsList = new List<Transactions>();
            transactionsList.Add(new Transactions() { Month = "Jan", Cath = "A", Year = "pre", Unit = 10 });
            transactionsList.Add(new Transactions() { Month = "Jan", Cath = "A", Year = "cur", Unit = 100 });
            transactionsList.Add(new Transactions() { Month = "Jan", Cath = "B", Year = "pre", Unit = 20 });
            transactionsList.Add(new Transactions() { Month = "Jan", Cath = "B", Year = "cur", Unit = 200 });
            transactionsList.Add(new Transactions() { Month = "Jan", Cath = "C", Year = "pre", Unit = 30 });
            transactionsList.Add(new Transactions() { Month = "Jan", Cath = "C", Year = "cur", Unit = 300 });
            transactionsList.Add(new Transactions() { Month = "Jan", Cath = "D", Year = "pre", Unit = 40 });
            transactionsList.Add(new Transactions() { Month = "Jan", Cath = "D", Year = "cur", Unit = 400 });
            transactionsList.Add(new Transactions() { Month = "Feb", Cath = "B", Year = "pre", Unit = 50 });
            transactionsList.Add(new Transactions() { Month = "Feb", Cath = "B", Year = "cur", Unit = 500 });

            var transactionsQuery =
                (from t in transactionsList
                group t by t.Month into newGroup
                orderby newGroup.Key descending
                select newGroup).ToList();

            var dictionaryList = new List<Dictionary<string, object>>();
            foreach (var nameGroup in transactionsQuery)
            {
                var data = new Dictionary<string, object>();
                data.Add("name", nameGroup.Key);
                foreach (var item in nameGroup)
                {                   
                    data.Add(string.Format("{0}{1}", item.Year, item.Cath), item.Unit);
                }
                dictionaryList.Add(data);
            }
           var ser = JsonConvert.SerializeObject(dictionaryList);
            Console.WriteLine(ser);
            Console.ReadLine();
        }
    }
}