简体   繁体   中英

Flattening JSON-file c# - reduce number of foreach-loops

I'm trying to deserialize a nested json-file that looks like the following sample: The output i want is "flat" table. I'm using .net 4.0 and I do not have the option of using third-party librabies like 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"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

From this I have genereated the following classes:

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; }
}

The output i want is a flat structure like the following:

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

I am able to obtain this with the following code, however this implies 3 foreach-loops which i'm afraid will give bad performance if there are ie 100.000 products with N colors and N chemicals each. Is there any other way to flatten this that will perform better using "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);
                    }
                }
            }

You want to start having the ToString done by the same objects:

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}";
    }
}

Then you just parse the whole chain and call it:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM