Konvertieren Sie JSON in CSV
JSON in CSV konvertieren
Aspose.Cells unterstützt die Konvertierung von einfachen sowie verschachtelten JSONs in CSV. Hierfür bietet die API die JsonLayoutOptions- und JsonUtility-Klassen. Die JsonLayoutOptions-Klasse bietet Optionen für das JSON-Layout wie IgnoreArrayTitle (ignoriert den Titel, wenn das Array eine Eigenschaft eines Objekts ist) oder ArrayAsTable (verarbeitet das Array als Tabelle). Die JsonUtility-Klasse verarbeitet das JSON unter Verwendung der mit der JsonLayoutOptions-Klasse festgelegten Layoutoptionen.
Der folgende Beispielcode zeigt die Verwendung der JsonLayoutOptions- und JsonUtility-Klassen zum Laden der Quell-JSON-Datei und erzeugt die Ausgabe-CSV-Datei.
Beispielcode
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Read JSON file | |
string str = File.ReadAllText(sourceDir + "SampleJson.json"); | |
// Create empty workbook | |
Workbook workbook = new Workbook(); | |
// Get Cells | |
Cells cells = workbook.Worksheets[0].Cells; | |
// Set JsonLayoutOptions | |
JsonLayoutOptions importOptions = new JsonLayoutOptions(); | |
importOptions.ConvertNumericOrDate = true; | |
importOptions.ArrayAsTable = true; | |
importOptions.IgnoreTitle = true; | |
JsonUtility.ImportData(str, cells, 0, 0, importOptions); | |
// Save Workbook | |
workbook.Save(outputDir + @"SampleJson_out.csv"); |