【发布时间】:2021-11-09 02:40:09
【问题描述】:
我有一个类似这样的 JSON 文件:
{
"id": 2,
"name": "I.1.A.2",
"activeFlag": true,
"recipients": [
{
"id": 3,
"identityName": "idenity1",
"fullName": "FullName1"
},
{
"id": 4,
"identityName": "identity2",
"fullName": "FullName2"
}
]
}
我需要使用 C# 和 dotnet Core 将其转换为与此类似的 CSV 输出。
"id","name","activeFlag","identityName"
"2","I.1.A.2","true","identity1;identity2"
但是,我只能得到 CSV 输出:
"id","name","activeFlag","recipients_0", "recipients_1"
"2","I.1.A.2","true","identity1","identity2"
这是生成上述输出的代码:
using (var csv = new ChoCSVWriter(".\\temp\\csvoutput.csv").WithFirstLineHeader()
)
{
using (var json = new ChoJSONReader(".\\temp\\tmpjson.json")
.Configure(c => c.ConvertToFlattenObject(arrayIndexSeparator: ';'))
.Configure(c => c.ArrayValueSeparator = ';')
.Configure(c => c.ArrayValueSeparator = ';')
.WithField("id", jsonPath: "$..id", isArray: false)
.WithField("recipients", jsonPath: "$..recipients[*]..identityName", isArray: true, fieldName: "recipients")
)
{
csv.Write(json);
}
}
现在,我正在使用 ChoEtl 库,但对其他选项/建议持开放态度。一直在寻找这个问题的答案,但还没有找到任何答案。抱歉,如果我还没有找到一些解决方案。我确实在这里尝试了类似的解决方案:How to output JSON array as a single field in CSV using ChoETL 但并不能完全满足我的需求。
【问题讨论】:
-
请包含创建当前输出的 C# 代码。
-
谢谢@JackA。!添加代码sn-p。