我需要將一個json轉換為csv。問題是我不能在嵌套的json結構中選擇我需要的所有內容。json檔案的例子:
{
"system"/span>: {
"created": "2021-08-01T13:33:37.123Z",
"by": "web".
},
"location": {
"id": 100,
"country": "DE"。
},
"order": [
{
"OrderID": 22,
"OrderName": "Soda",
"OrderArticles": [
{
"尺寸": 33,
"ProductName": "Coke",
"ProductId": "999".
},
{
"尺寸": 66,
"ProductName": "Fanta",
"ProductId": "888".
},
{
"尺寸": 50,
"ProductName": "Pepsi",
"ProductId": "444".
}
],
"ProcessId": 1001,
"Date": "2021-08-02"
},
{
"OrderID": 23,
"OrderName": "飲料",
"OrderArticles": [
{
"尺寸": 44,
"ProductName": "Coke",
"ProductId": "999".
}
],
"ProcessId": 1002,
"Date": "2021-08-03"
}
]
}
這是我想要的輸出:
這是我想要的輸出。
created;by;id;country;orderID;orderName;size;ProductName;ProductId
2021-08-01T13:33:37. 123Z;web;100;DE;22;Soda;33;Coke;999?
2021-08-01T13:33:37. 123Z;web;100;DE;22;Soda;66;Fanta;888。
2021-08-01T13:33:37。 123Z;web;100;DE;22;Soda;50;Pepsi;444?
2021-08-01T13:33:37. 123Z;web;100;DE;23;飲料;44;可樂;999。
我可以自行獲得創建和通過的值以及OrderArticles的值。我只是不知道如何把它們連在一起。這是我用來獲得結果的代碼,但分成了兩個不同的結果:
using (var r = new ChoJSONReader(inBlob). WithJSONPath("$...order[*]").AllowComplexJSONPath(true))
{
return (r.SelectMany(r1 => ((dynamic[])r1.OutputArticles).Select(r2 => new)
{
r1.OrderID,
r1.OrderName,
r1.尺寸。
r1.ProductName,
r1.ProductId
})));
}
using (var r = new ChoJSONReader(inBlob)。 WithJSONPath("$").AllowComplexJSONPath(true)
{
return (r.Select(r1 => new)
{
r1.system.created,
r1.system.by})
}));
}
uj5u.com熱心網友回復:
由于你需要system.created, system.by, location.id, location.country欄位,你必須從root加載整個json,然后為csv組成預期的物件
下面是作業樣本(采取最新的nuget包)
方法 1: (使用動態模型)StringBuilder csv = new StringBuilder()。
使用(var r = new ChoJSONReader("*** YOUR JSON FILE PATH**")
.JsonSerializationSettings(s => s.DateParseHandling = DateParseHandling.None)
)
{
using (var w = new ChoCSVWriter(csv))
.WithDelimiter(";")
.WithFirstLineHeader())
{
w.Write(r.SelectMany(root =>
((Array)root.order).Cast<dynamic> ()
.SelectMany(order => ((Array)order.OrderArticle).Cast<動態>()
.Select(orderarticle => new)
{
root.system.created,
root.system.by,
root.location.id,
order.OrderID,
order.OrderName,
orderarticle.Size,
orderarticle.ProductName,
orderarticle.ProductId,
})
)
)
);
}
}
Console.WriteLine(csv.ToString() )。
輸出:
created;by;id;orderID;orderName;Size;ProductName;ProductId
2021-08-01T01:33:37. 123Z;web;100;22;Soda;33;Coke;999?
2021-08-01T01:33:37. 123Z;web;100;22;Soda;66;Fanta;888?
2021-08-01T01:33:37. 123Z;web;100;22;Soda;50;Pepsi;444?
2021-08-01T01:33:37. 123Z;web;100;23;飲料;44;可樂;999?
方法2:使用POCO模型
。定義與輸入JSON匹配的POCO物件
public class System
{
[]
public string Created { get; set; }
[]
public string By { get; set; }
}
public class Location
{
[]
public int Id { get; set; }
[]
public string Country { get; set; }
}
public class OrderArticle
{
[]
public int Size { get; set; }
[]
public string ProductName { get; set; }
[]
public string ProductId { get; set; }
}
public class Order
{
[]
public int OrderID { get; set; }
[]
public string OrderName { get; set; }
[]
public List<OrderArticle> OrderArticles { get; set; }
[]
public int ProcessedId { get; set; }
[]
public string Date { get; set; }
}
public class OrderRoot
{
[]
public System 系統 { get; set; }
[
然后使用下面的代碼加載json并以預期的格式輸出CSV
。StringBuilder csv = new StringBuilder();
using (var r = new ChoJSONReader<OrderRoot>("*** YOUR JSON FILE PATH**")
.UseJsonSerialization()
)
{
using (var w = new ChoCSVWriter(csv)
.WithDelimiter(";")
.WithFirstLineHeader())
{
w.Write(r.SelectMany(root =>
root.Orders
.SelectMany(order => order.OrderArticle
.Select(orderarticle => new。
{
created = root.System.Created,
by = root.System.By,
id = root.Location.Id,
order.OrderID,
order.OrderName,
orderarticle.Size,
orderarticle.ProductName,
orderarticle.ProductId,
})
)
)
);
}
}
Console.WriteLine(csv.ToString() )。
方法3:簡化的動態模型方法
StringBuilder csv = new StringBuilder()。
使用(var r = new ChoJSONReader("*** YOUR JSON FILE PATH**")
.WithField("created", jsonPath: "$.system.created", isArray: false, valueConverter: o => ((DateTime)o).ToString("yyyy-MM-ddThh:mm:ss.ffffZ")
.WithField("by", jsonPath: "$.system.by", isArray: false)
.WithField("id", jsonPath: "$.location.id", isArray: false)
.WithField("country", jsonPath: "$.location.country", isArray: false)
.WithField("OrderID")
.WithField("OrderName")
.WithField("Size")
.WithField("ProductName")
.WithField("ProductId")
.Configure(c => c.FlattenNode = true)
)
{
using (var w = new ChoCSVWriter(csv))
.WithDelimiter(";")
.WithFirstLineHeader())
{
w.Write(r)。
}
}
Console.WriteLine(csv.ToString())。
方法4:甚至遠遠簡化的動態模型方法
StringBuilder csv = newStringBuilder()。
使用(var r = new ChoJSONReader("*** YOUR JSON FILE PATH**")
.Configure(c => c.FlattenNode = true)
.JsonSerializationSettings(s => s.DateParseHandling = DateParseHandling.None)
)
{
using (var w = new ChoCSVWriter(csv))
.WithDelimiter(";")
.WithFirstLineHeader()
.Configure(c => c.IgnoreDictionaryFieldPrefix = true)
)
{
w.Write(r)。
}
}
Console.WriteLine(csv.ToString())。
示例fiddle。https://dotnetfiddle.net/VCezp8
uj5u.com熱心網友回復:
這是我的解決方案。
這是我的資料。
這是我的資料模型:
這是我的資料模型。
using System.Text.Json.Serialization;
namespace JsonToCSV.Models。
// Root myDeserializedClass = JsonSerializer.Deserialize<Root> (myJsonResponse);
public class System
{
[JsonPropertyName("created")]
public string Created { get; set; }
[JsonPropertyName("by")]
public string By { get; set; }
}
public class Location
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("country")]
public string Country { get; set; }
}
public class OrderArticle
{
[JsonPropertyName("Size")]
public int Size { get; set; }
[JsonPropertyName("ProductName")]
public string ProductName { get; set; }
[JsonPropertyName("ProductId")]
public string ProductId { get; set; }
}
public class Order
{
[JsonPropertyName("OrderID")]
public int OrderID { get; set; }
[JsonPropertyName("OrderName")]
public string OrderName { get; set; }
[JsonPropertyName("OrderArticles")]
public List<OrderArticle> OrderArticles { get; set; }
[JsonPropertyName("ProcessId")]
public int ProcessedId { get; set; }
[JsonPropertyName("Date")]
public string Date { get; set; }
}
public class Root
{
[JsonPropertyName("system")]
public System { get; set; }
[JsonPropertyName("location")]
public Location Location { get; set; }
[JsonPropertyName("order")]
public List<Order> Orders { get; set; }
而這里是業務邏輯(如果你愿意,我可以用LINQ來代替):
using System.Text.Json。
using JsonToCSV.Models;
var dataAsText = File.ReadAllText("data.json") 。
var data = JsonSerializer.Deserialize<Root>(dataAsText)。
var csv = new List<string> { "created;by; id; country; OrderID; OrderName; Size; ProductName; ProductId" };
foreach (var order in data.Orders)
{
foreach (var orderArticle in order.OrderArticles)
{
csv.Add(String.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8}",
data.System.Credated,
data.System.By,
data.Location.Id,
data.Location.Country,
order.OrderID,
order.OrderName,
orderArticle.Size,
orderArticle.ProductName。
orderArticle.ProductId
));
}
}
File.WriteAllLines("data.csv"/span>, csv)。
創建帶有內容的.csv檔案:
created;by;id;country;orderID;orderName;size;ProductName;ProductId
2021-08-01T13:33:37. 123Z;web;100;DE;22;Soda;33;Coke;999?
2021-08-01T13:33:37. 123Z;web;100;DE;22;Soda;66;Fanta;888。
2021-08-01T13:33:37。 123Z;web;100;DE;22;Soda;50;Pepsi;444?
2021-08-01T13:33:37. 123Z;web;100;DE;23;飲料;44;可樂;999。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/310098.html
標籤:
