Konvertera JSON till CSV

Contents
[ ]

Aspose.Cells stöder konvertering av enkla såväl som inbäddade JSON till CSV. För detta tillhandahåller API:et klasserna JsonLayoutOptions och JsonUtility. Klassen JsonLayoutOptions tillhandahåller alternativ för JSON-layout som IgnoreArrayTitle (ignorerar titeln om arrayen är en egenskap hos ett objekt) eller ArrayAsTable (behandlar arrayen som en tabell). Klassen JsonUtility behandlar JSON med användning av layoutalternativen som är inställda med hjälp av klassen JsonLayoutOptions.

Följande kodexempel visar användningen av klasserna JsonLayoutOptions och JsonUtility för att ladda käll-JSON-filen och genererar utdata-CSV-filen.

Exempelkod

// 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");