使用LINQ将此对象列表转换为json

时间:2015-03-10 20:39:10

标签: c# json linq highcharts

我正在尝试将一些数据转换为HighCharts库期望其系列数据的json对象。格式如下:

var series = [
        {
            name : "Duplicate Transactions"
            data : [190, 107]
        },
        {
            name : "Unique Transactions"
            data : [55, 51]
        }
    ]

我的后端代码中有这样的对象列表:

UsageData usageData = new UsageData()
{
    {UsageType = "FIND", DupTrans  = 190, UniqTrans = 55 },
    {UsageType = "PARTS", DupTrans  = 107, UniqTrans = 51 }
};

到目前为止,我有这个:

var dup = usageData.GroupBy(d => d.DuplicateTransactions)
    .Select(g => new
    {
        name = "Duplicate Transactions",
        usageData = g.Select(c => c.DuplicateTransactions).ToArray()
    });

var uniq = usageData.GroupBy(d => d.UniqueTransactions)
    .Select(g => new
    {
        name = "Unique Transactions",
        usageData = g.Select(c => c.UniqueTransactions).ToArray()
    });

但我得到的输出并不理想:

[
    {
        "name" : "Duplicate Transactions",
        "data" : [190]
    },
    {
        "name" : "Duplicate Transactions",
        "data" : [107]
    }
]

[
    {
        "name" : "Unique Transactions",
        "data" : [55]
    },
    {
        "name" : "Unique Transactions",
        "data" : [51]
    }
]

3 个答案:

答案 0 :(得分:2)

您可以再添加一个将被序列化的数据类。请参阅下面的代码。

 class Output
{
    public string Name { get; set; }
    public int[] DupTrans { get; set; }
}

class UsageData
{
    public string UsageType { get; set; }
    public int DupTrans { get; set; }
    public int UniqTrans { get; set; }
}
 List<UsageData> usageData = new List<UsageData>()
        {
            new UsageData(){UsageType = "FIND", DupTrans  = 190, UniqTrans = 55 },
            new UsageData(){UsageType = "PARTS", DupTrans  = 107, UniqTrans = 51 }
        };

        var myObj = new { Name = "Duplicate Transactions", DupTrans = usageData.Select(x => x.DupTrans).ToArray() };
        string str = JsonConvert.SerializeObject(myObj);

答案 1 :(得分:1)

这是我的变体:

var usageData = new List<UsageData>()
{
    new UsageData {UsageType = "FIND", DupTrans  = 190, UniqTrans = 55 },
    new UsageData {UsageType = "PARTS", DupTrans  = 107, UniqTrans = 51 }
};

var dup = usageData
        .GroupBy(p => "Duplicate Transactions", p => p.DupTrans)
        .Select(g => new {
            name = "Duplicate Transactions",
            data = g.ToList()
        }).First();

Console.WriteLine(dup.name);
Console.WriteLine(string.Join(",", dup.data));

var uniq  = usageData
            .GroupBy(p => "Unique Transactions", p => p.UniqTrans)
            .Select(g => new {
                name = "Unique Transactions",
                data = g.ToList()
            }).First();

Console.WriteLine(uniq.name);
Console.WriteLine(string.Join(",", uniq.data));

此示例使用常量键作为组键,因此所有项目将分组为一个,然后它可以与序列化程序一起使用。

以下代码使用Console.WriteLine生成此类输出:

Duplicate Transactions
190,107
Unique Transactions
55,51

以下是DotNetFiddle的工作示例 - https://dotnetfiddle.net/IEwoDC

答案 2 :(得分:0)

你可能想要的是:

var grp = usageData
                .GroupBy(u => u.name)
                .Select(g => new 
                    { 
                        name = g.Key, 
                        data = g.Select(u => u.data).ToArray() 
                    });

我的基础是你最初提供的数据(如上面的查询输出与下面的数据匹配):

var series = [
    {
        name : "Duplicate Transactions"
        data : [190, 107]
    },
    {
        name : "Unique Transactions"
        data : [55, 51]
    }
]

这会假设您的课程是这样的:

class serie
{
    public string name;
    public int data;
}

你的数据是这样的:

var usageData = new[] 
{
    new serie { name = "Duplicate Transactions", data = 190 },
    new serie { name = "Duplicate Transactions", data = 107 },
    new serie { name = "Unique Transactions", data = 55 },
    new serie { name = "Unique Transactions", data = 51 },
}

您设计课程的方式与上述内容不一致:

UsageData usageData = new UsageData()
{
    {UsageType = "FIND", DupTrans  = 190, UniqTrans = 55 },
    {UsageType = "PARTS", DupTrans  = 107, UniqTrans = 51 }
};  

所以我不确定我是否有所帮助,但我提供的示例肯定会产生您最初发布的JSON结果。

我没有在VS中测试过你的数据,所以告诉我你是否有测试这个或理解的麻烦。