Konvertieren Sie JSON in CSV
Aspose.Cells unterstützt die Konvertierung von einfachen sowie verschachtelten JSON-Dateien in CSV. Dafür stellt die API die Klassen JsonLayoutOptions und JsonUtility zur Verfügung. Die Klasse JsonLayoutOptions bietet die Optionen für das Layout des JSON wie IgnoreArrayTitle (ignoriert den Titel, wenn das Array eine Eigenschaft eines Objekts ist) oder ArrayAsTable (verarbeitet das Array als Tabelle). Die Klasse JsonUtility verarbeitet das JSON mit den mit der Klasse JsonLayoutOptions festgelegten Layoutoptionen.
Das folgende Codebeispiel zeigt die Verwendung der Klassen JsonLayoutOptions und JsonUtility zum Laden der Quell-JSON-Datei und erzeugt die Ausgabedatei im CSV-Format.
Beispielcode
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
//Source directory | |
String sourceDir = Utils.Get_SourceDirectory(); | |
//Output directory | |
String outputDir = Utils.Get_OutputDirectory(); | |
// Read JSON file | |
String str = new String(Files.readAllBytes(Paths.get(sourceDir + "SampleJson.json"))); | |
// Create empty workbook | |
Workbook workbook = new Workbook(); | |
// Get Cells | |
Cells cells = workbook.getWorksheets().get(0).getCells(); | |
// Set JsonLayoutOptions | |
JsonLayoutOptions importOptions = new JsonLayoutOptions(); | |
importOptions.setConvertNumericOrDate(true); | |
importOptions.setArrayAsTable(true); | |
importOptions.setIgnoreArrayTitle(true); | |
importOptions.setIgnoreObjectTitle(true); | |
JsonUtility.importData(str, cells, 0, 0, importOptions); | |
// Save Workbook | |
workbook.save(outputDir + "SampleJson_out.csv"); |