如何使用 Cinchoo ETL 的 ChoCSVWriter 写入数组的所有值?
How I write all values of array using ChoCSVWriter of Cinchoo ETL?
写入 CSV 文件时,它没有值 red
和 small
示例:
输入 json 文件:
[
{
"id": 1,
"name": "Mike",
"features": {
"colors": [
"blue"
],
"sizes": [
"big"
]
}
},
{
"id": 1,
"name": "Jose",
"features": {
"colors": [
"blue",
"red"
],
"sizes": [
"big",
"small"
]
}
}
]
输出 csv 文件:
id;name;features_colors_0;features_sizes_0
1;Mike;blue;big
1;Jose;blue;big
代码:
using ChoETL;
using System.IO;
namespace TestJsonToCsv
{
class Program
{
static void Main(string[] args)
{
var jsonPath = @"C:\Users\xxx\Downloads\test_array.json";
var csvPath = Path.ChangeExtension(jsonPath, "csv");
var config = new ChoJSONRecordConfiguration();
config.Encoding = System.Text.Encoding.UTF8;
using (var r = new ChoJSONReader(jsonPath, config))
{
using (var w = new ChoCSVWriter(csvPath).WithFirstLineHeader())
{
w.Write(r);
}
}
}
}
}
库版本:
ChoETL-1.2.1.14
ChoETL.JSON - 1.2.1.14
问题参考 - https://github.com/Cinchoo/ChoETL/issues/138
您将必须指示作者使用 WithMaxScanRows
扫描节点以发现大小
这是工作示例
string json = @"
[
{
""id"": 1,
""name"": ""Mike"",
""features"": {
""colors"": [
""blue""
],
""sizes"": [
""big""
]
}
},
{
""id"": 1,
""name"": ""Jose"",
""features"": {
""colors"": [
""blue"",
""red""
],
""sizes"": [
""big"",
""small""
]
}
}
]";
StringBuilder csv = new StringBuilder();
using (var r = ChoJSONReader.LoadText(json))
{
using (var w = new ChoCSVWriter(csv)
.WithFirstLineHeader()
.WithMaxScanRows(2)
.ThrowAndStopOnMissingField(false)
)
{
w.Write(r);
}
}
Console.WriteLine(csv.ToString());
输出:
id,name,features_colors_0,features_colors_1,features_sizes_0,features_sizes_1
1,Mike,blue,,big,
1,Jose,blue,red,big,small
写入 CSV 文件时,它没有值 red
和 small
示例:
输入 json 文件:
[
{
"id": 1,
"name": "Mike",
"features": {
"colors": [
"blue"
],
"sizes": [
"big"
]
}
},
{
"id": 1,
"name": "Jose",
"features": {
"colors": [
"blue",
"red"
],
"sizes": [
"big",
"small"
]
}
}
]
输出 csv 文件:
id;name;features_colors_0;features_sizes_0
1;Mike;blue;big
1;Jose;blue;big
代码:
using ChoETL;
using System.IO;
namespace TestJsonToCsv
{
class Program
{
static void Main(string[] args)
{
var jsonPath = @"C:\Users\xxx\Downloads\test_array.json";
var csvPath = Path.ChangeExtension(jsonPath, "csv");
var config = new ChoJSONRecordConfiguration();
config.Encoding = System.Text.Encoding.UTF8;
using (var r = new ChoJSONReader(jsonPath, config))
{
using (var w = new ChoCSVWriter(csvPath).WithFirstLineHeader())
{
w.Write(r);
}
}
}
}
}
库版本:
ChoETL-1.2.1.14
ChoETL.JSON - 1.2.1.14
问题参考 - https://github.com/Cinchoo/ChoETL/issues/138
您将必须指示作者使用 WithMaxScanRows
这是工作示例
string json = @"
[
{
""id"": 1,
""name"": ""Mike"",
""features"": {
""colors"": [
""blue""
],
""sizes"": [
""big""
]
}
},
{
""id"": 1,
""name"": ""Jose"",
""features"": {
""colors"": [
""blue"",
""red""
],
""sizes"": [
""big"",
""small""
]
}
}
]";
StringBuilder csv = new StringBuilder();
using (var r = ChoJSONReader.LoadText(json))
{
using (var w = new ChoCSVWriter(csv)
.WithFirstLineHeader()
.WithMaxScanRows(2)
.ThrowAndStopOnMissingField(false)
)
{
w.Write(r);
}
}
Console.WriteLine(csv.ToString());
输出:
id,name,features_colors_0,features_colors_1,features_sizes_0,features_sizes_1
1,Mike,blue,,big,
1,Jose,blue,red,big,small