Converti JSON in CSV
Aspose.Cells supporta la conversione di JSON semplici e nidificati in CSV. A tale scopo, l’API fornisce le classi JsonLayoutOptions e JsonUtility. La classe JsonLayoutOptions fornisce le opzioni per la struttura JSON come IgnoreArrayTitle (ignora il titolo se l’array è una proprietà di un oggetto) o ArrayAsTable (elabora l’array come una tabella). La classe JsonUtility elabora il JSON utilizzando le opzioni di layout impostate con la classe JsonLayoutOptions.
Il seguente esempio di codice dimostra l’uso delle classi JsonLayoutOptions e JsonUtility per caricare il file JSON di origine e generare l file CSV di output.
Codice di esempio
// 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"); |