我有一张物品清单。它有四个属性。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,但我很难创建一个可以转换为所需格式的类。
发布于 2016-02-26 04:06:15
您可以使用一系列字典。
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变量。
希望这能让你更接近你的解决方案。
发布于 2016-02-26 06:29:17
以下是我的实现:
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();
}
}
}https://stackoverflow.com/questions/35643183
复制相似问题