我正试图将一个JSON文件(一个Microsoft.Graph.Event)转换为CSV文件。我正在使用Cinchoo ETL来做这件事。这里是我所指的URL。
https:/www.codeproject.comArticles1193650Cinchoo-ETL-Quick-Start-Converting-JSON-to-CSV-Fil
这是我的代码。
using (var csv = new ChoCSVWriter(path + calendarId + ".csv").WithFirstLineHeader())
{
using (var json = new ChoJSONReader(path + calendarId + ".json")
.WithField("id")
.WithField("iCalUId")
.WithField("isAllDay")
.WithField("isCancelled")
.WithField("isOrganizer")
.WithField("isOnlineMeeting")
.WithField("onlineMeetingProvider")
.WithField("type")
.WithField("startTime", jsonPath: "$.start.dateTime")
.WithField("endTime", jsonPath: "$.end.dateTime")
.WithField("location", jsonPath: "$.location.displayname")
.WithField("locationType", jsonPath: "$.location.locationType")
.WithField("organizer", jsonPath: "$.organizer.emailAddress.name")
.WithField("recurrence", jsonPath: "$.recurrence.pattern.type")
)
{
csv.Write(json);
}
}
虽然我得到了一个CSV文件 大部分的标题和值都是正确的 但有些是奇怪的。一些标题在后面得到了一个"_0",一些值只是重复了前一列,而不是它应该是什么。csv文件的快照
我已经检查了我事先写好的JSON文件,但他们就很好。我使用的是.Net Core 3.1。
我只是一个初学者,任何帮助或建议都非常感激。谢谢你的帮助。
EDIT添加CSV的快照,其中一些值只是重复之前的列 "startTime",而不是它应该是什么。
这是JSON文件的一部分
"start": {
"dateTime": "2020-05-17T00:00:00.0000000",
"timeZone": "UTC",
"@odata.type": "microsoft.graph.dateTimeTimeZone"
},
"end": {
"dateTime": "2020-05-18T00:00:00.0000000",
"timeZone": "UTC",
"@odata.type": "microsoft.graph.dateTimeTimeZone"
},
"location": {
"displayName": "asdfads",
"locationType": "default",
"uniqueId": "b0fd5377-937d-4fb2-b70a-0a696972b46c",
"uniqueIdType": "locationStore",
"@odata.type": "microsoft.graph.location"
},
在这里,我取了一个JSON样本,来自
https:/docs.microsoft.comen-usgraphapicalendar-post-events?view=graph-rest-1.0&tabs=http。
用于测试它。
下面是代码,使用 ChoETL v1.2.0.2 (最新)。
StringBuilder csv = new StringBuilder();
using (var w = new ChoCSVWriter(csv)
.WithFirstLineHeader()
)
{
using (var r = new ChoJSONReader(@"C:\Projects\GitHub\ChoETL\src\Test\ChoJSONReaderTest\sample41.json")
.WithField("id")
.WithField("iCalUId")
.WithField("isAllDay")
.WithField("isCancelled")
.WithField("isOrganizer")
.WithField("isOnlineMeeting")
.WithField("onlineMeetingProvider")
.WithField("type")
.WithField("startTime", jsonPath: "$.start.dateTime", isArray: false)
.WithField("endTime", jsonPath: "$.end.dateTime", isArray: false)
.WithField("location", jsonPath: "$.location.displayname")
.WithField("locationType", jsonPath: "$.location.locationType", isArray: false)
.WithField("organizer", jsonPath: "$.organizer.emailAddress.name", isArray: false)
.WithField("recurrence", jsonPath: "$.recurrence.pattern.type")
)
{
w.Write(r);
}
}
Console.WriteLine(csv.ToString());
输出:
id,iCalUId,isAllDay,isCancelled,isOrganizer,isOnlineMeeting,onlineMeetingProvider,type,startTime,endTime,location,locationType,organizer,recurrence
AAMkAGViNDU7zAAAAA7zAAAZb2ckAAA=,040000008200E641B4C,False,False,True,False,unknown,singleInstance,3/15/2019 12:00:00 PM,3/15/2019 2:00:00 PM,,default,Megan Bowen,
UPDATE:这里是更新后的代码,以改变字段的顺序,并得到与会者的计数。
StringBuilder csv = new StringBuilder();
using (var w = new ChoCSVWriter(csv)
.WithFirstLineHeader()
)
{
using (var r = new ChoJSONReader(@"C:\Projects\GitHub\ChoETL\src\Test\ChoJSONReaderTest\sample41.json")
.WithField("startTime", jsonPath: "$.start.dateTime", isArray: false)
.WithField("endTime", jsonPath: "$.end.dateTime", isArray: false)
.WithField("id")
.WithField("iCalUId")
.WithField("isAllDay")
.WithField("isCancelled")
.WithField("isOrganizer")
.WithField("isOnlineMeeting")
.WithField("onlineMeetingProvider")
.WithField("type")
.WithField("location", jsonPath: "$.location.displayname")
.WithField("locationType", jsonPath: "$.location.locationType", isArray: false)
.WithField("organizer", jsonPath: "$.organizer.emailAddress.name", isArray: false)
.WithField("recurrence", jsonPath: "$.recurrence.pattern.type")
.WithField("attendees", jsonPath: "$.attendees[*]", valueConverter: o => ((IList)o).Count)
)
{
w.Write(r);
}
}
Console.WriteLine(csv.ToString());