[C#]创建使用 LINQ 的 JSON 数组

标签: json Linq C#
发布时间: 2017/2/27 7:20:02
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个列表的对象从数据库返回的序列化使用 JSON.NET 时看起来像这样︰

"[{"Percentage":0.78, "PartCode":"D40", "InspectionCode":"292", "Make":"TOYOTA"}]
{"Percentage":0.18, "PartCode":"6N", "InspectionCode":"292", "Make":"GM"},
{"Percentage":0.57, "PartCode":"6N", "InspectionCode":"F", "Make":"GM"},
{"Percentage":0.49, "PartCode":"D40", "InspectionCode":"F", "Make":"TOYOTA"},
{"Percentage":0.09, "PartCode":"785", "InspectionCode":"KB", "Make":"CHRYSLER"},
{"Percentage":0.09, "PartCode":"705", "InspectionCode":"KB", "Make":"FORD"},
{"Percentage":0.18, "PartCode":"D40", "InspectionCode":"KB", "Make":"TOYOTA"},
{"Percentage":0.61, "PartCode":"D40", "InspectionCode":"KB", "Make":"TOYOTA"},
{"Percentage":0.39, "PartCode":"705", "InspectionCode":"SB", "Make":"FORD"},
{"Percentage":0.31, "PartCode":"6N", "InspectionCode":"SB", "Make":"GM"},
{"Percentage":0.21, "PartCode":"AW7", "InspectionCode":"XE1", "Make":"CHRYSLER"},
{"Percentage":0.27, "PartCode":"705", "InspectionCode":"XE1", "Make":"FORD"},
{"Percentage":0.28, "PartCode":"UX", "InspectionCode":"XE1", "Make":"FORD"},
{"Percentage":0.56, "PartCode":"D40", "InspectionCode":"XE1", "Make":"TOYOTA"}]"

我需要在这种格式要传递给 HighCharts 中创建两个 JSON 数组︰

var categories = [
{name: "Toyota", categories: ['D40']},
{name: "GM", categories: ['6N']},
{name: "FORD", categories: ['705', 'UX']},
{name: "CHRYSLER", categories: ['AW7','785']}];

var series = [
{name: "292", data = [0.78, 0.18]}
{name: "F", data = [0.57, 0.49]},
{name: "KB", data = [0.09, 0.09, 0.18, 0.61]},
{name: "SB", data = [0.39, 0.31]},
{name: "XE1", data = [0.21, 0.27, 0.28, 0.56]}];

到目前为止,我做嵌套的分组的数据,因为使和 PartCode 是分层数据。

var query = from d in sortedData
            group d by d.Make into newgroup1
            from newgroup2 in
                (from e in newgroup1
                 group e by e.PartCode)
            group newgroup2 by newgroup1.Key;

我能够看到在分层格式中使用的数据︰

foreach (var outergroup in query)
{
    System.Diagnostics.Debug.WriteLine(outergroup.Key);
    foreach (var innergroup in outergroup)
    {
        System.Diagnostics.Debug.WriteLine(innergroup.Key);
        foreach (var innerGroupElement in innergroup)
        {
            System.Diagnostics.Debug.WriteLine("\t\t{0} {1}", innerGroupElement.InspectionCode, innerGroupElement.Percentage);
        }
    }
}

但是,我有很难理解怎样做进一步去所需的 JSON 数组。我有哪些步骤进一步采取为了实现这一目标?

解决方法 1:

这将让你你想要什么。一旦你按使或 InspectionCode 进行分组,然后那子列表中的所有项目将都包含您正在寻找的数据。

var categories = sortedData.GroupBy(d => d.Make)
                           .Select(g => new 
                           { 
                               name = g.Key, 
                               categories = g.Select(x => x.PartCode).ToArray() 
                           });

var series = sortedData.GroupBy(d => d.InspectionCode)
                       .Select(g => new 
                       { 
                           name = g.Key, 
                           data = g.Select(x => x.Percentage).ToArray() 
                       });

var categoriesAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(categories);
var seriesAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(series);

如果你把数据已经在内存中,将其转换为 Json 是一样容易的最后两行,使用Json.NET。如果你在通过 WebAPI 端点线发出这,然后你就可以了您返回一个列表,将类别或系列的列表对象不具有将它们转换为 JSON 的端点。

[HttpGet]
public HttpResponseMessage GetCategories()
{
    var categories = GetCategoriesUsingAboveCode();
    return Request.CreateResponse(HttpStatusCode.OK, categories);
}
官方微信
官方QQ群
31647020