c# - 展平 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);
}
}
}
解决方案
您想开始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);
推荐阅读
- selenium-webdriver - 如何在网站上创建我自己的 xpath - Selenium
- excel - Excel VBA在过滤范围意外结果上设置值
- url-rewriting - How to rewrite URL in Oracle Content and Experience
- html - 如何向 itemTpl 中定义的组件添加侦听器?
- arduino - Arduino + SIM800A 消息被截断
- javascript - Sequelize:声明了外键,但没有出现在表中
- json - 如何在 google firebase 上启用禁用字符?
- amazon-web-services - Powershell AWS 创建一个分配前缀列表的安全组
- ansible - 如何使用 ansible 移动 centos 用户的 homedir?
- algorithm - 在没有实际排序列表的情况下无法以排序方式打印列表