首页 > 解决方案 > 展平 JSON 文件 c# - 减少 foreach 循环的数量

问题描述

我正在尝试反序列化一个嵌套的 json 文件,该文件类似于以下示例:我想要的输出是“平面”表。我正在使用 .net 4.0,但我无法选择使用第三方库,如 json.net。

{
    "responsetime": 33,
    "products": {
        "totalProducts": 25,
        "products": [
            {
                "id": 1,
                "name": "Bike One",
                "colors": [
                    {
                        "colorId": 44,
                        "name": "green",
                        "chemicals": [
                            {
                                "chemicalId": 99,
                                "chemicalName": "abc"
                            },
                            {
                                "chemicalId": 45,
                                "chemicalName": "bcd"
                            }
                        ]
                    },
                    {
                        "colorId": 42,
                        "name": "blue",
                        "chemicals": [
                            {
                                "chemicalId": 96,
                                "chemicalName": "def"
                            },
                            {
                                "chemicalId": 22,
                                "chemicalName": "lkj"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

由此我生成了以下类:

public class ResponseObject
{
    public int responsetime { get; set; }
    public Products products { get; set; }
}

public class Products
{
    public int totalProducts { get; set; }
    public Product[] products { get; set; }
}

public class Product
{
    public int id { get; set; }
    public string name { get; set; }
    public Color[] colors { get; set; }
}

public class Color
{
    public int colorId { get; set; }
    public string name { get; set; }
    public Chemical[] chemicals { get; set; }
}

public class Chemical
{
    public int chemicalId { get; set; }
    public string chemicalName { get; set; }
}

我想要的输出是一个平面结构,如下所示:

1 Bike One 44 green 99 abc 
1 Bike One 44 green 45 bcd 
1 Bike One 42 blue  96 def 
1 Bike One 42 blue  22 lkj

我可以使用下面的代码来获得它,但是这意味着 3 个 foreach 循环,如果有 100.000 种产品,每种产品都有 N 种颜色和 N 种化学品,我担心这会导致性能不佳。有没有其他方法可以使用“vanilla”.net 将其展平?

            String jsonFileContent = File.ReadAllText(@"C:\example.json"); 
            JavaScriptSerializer js = new JavaScriptSerializer();
            js.MaxJsonLength = Int32.MaxValue;
            ResponseObject rspns = js.Deserialize<ResponseObject>(jsonFileContent);

            foreach (var product in rspns.products.products)
            {
                foreach (var color in product.colors)
                {
                    foreach (var chemical in color.chemicals)
                    {
                        Console.WriteLine(product.id);
                        Console.WriteLine(product.name);
                        Console.WriteLine(color.colorId);
                        Console.WriteLine(color.name);
                        Console.WriteLine(chemical.chemicalId);
                        Console.WriteLine(chemical.chemicalName);
                    }
                }
            }

标签: c#arraysjson

解决方案


您想开始ToString由相同的对象完成:

public class Products
{
    public int totalProducts { get; set; }
    public Product[] products { get; set; }

    public override string ToString(){
        // this iterates all products and stacks their string representation
        var productsStrings = products.Select(x => x.ToString());
        return productsStrings.Aggregate("", (a, n) => a + n + "\n").trimEnd();
    }
}

public class Product
{
    public int id { get; set; }
    public string name { get; set; }
    public Color[] colors { get; set; }

    public override string ToString(){
        // this gets all color strings and prepends the product string
        // "1 Bike" + product (repeated CO times)
        var colorSubstrings = colors.Select(x => x.GetSubstrings());
        var appendOnStrings = colorSubstrings.Select(x => $"{id} {name} {x}");
        return appendOnStrings.Aggregate("", (a, n) => a + n + "\n").trimEnd();
    }
}

public class Color
{
    public int colorId { get; set; }
    public string name { get; set; }
    public Chemical[] chemicals { get; set; }

    public string[] GetSubstrings(){
        // this gets all chemicals strings and prepends the color string
        // "44 green" + chemicalString (repeated CH times)
        return chemicals.Aggregate("", (a, chemical) => a + $"{colorId} {name} {chemical.ToString()} \n").trimEnd();
    }
}

public class Chemical
{
    public int chemicalId { get; set; }
    public string chemicalName { get; set; }

    public override string ToString(){
        // this produces a string like -> "99 abc"
        return $"{chemicalId} {chemicalName}";
    }
}

然后你只需解析整个链并调用它:

String jsonFileContent = File.ReadAllText(@"C:\example.json");
JavaScriptSerializer js = new JavaScriptSerializer();
js.MaxJsonLength = Int32.MaxValue;

// deserializing JSON to rspns
ResponseObject rspns = js.Deserialize<ResponseObject>(jsonFileContent);

// result will contain the table with all products, colors and chemicals
var result =  rspns.products.ToString();

// printing only once will surely improve the performances
Console.WriteLine(result);

推荐阅读