使用 C# 对列表中的值进行分组并将输出转换为 JSON
Grouping values in a list and convert the output into JSON using C#
我有一个对象列表。其中有 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 输出。我在 JSON 对象中创建 属性,方法是在类别名称前加上 cur
和 pre
关键字,它们代表的值来自 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,但我正在努力创建一个可以转换为所需格式的 class。
您可以使用字典数组。
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();
}
}
}
我有一个对象列表。其中有 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 输出。我在 JSON 对象中创建 属性,方法是在类别名称前加上 cur
和 pre
关键字,它们代表的值来自 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,但我正在努力创建一个可以转换为所需格式的 class。
您可以使用字典数组。
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();
}
}
}