Konvertera JSON till CSV
Contents
[
Hide
]
Aspose.Cells stöder konvertering av såväl enkla som kapslade JSON till CSV. För detta tillhandahåller APIJsonLayoutOptionsochJsonUtilityklasser. DeJsonLayoutOptionsklass ger alternativen för JSON layout somIgnoreArrayTitle(ignorerar titeln om arrayen är en egenskap hos ett objekt) ellerArrayAsTable(behandlar arrayen som en tabell). DeJsonUtilityklass bearbetar JSON med hjälp av layoutalternativen som ställts in medJsonLayoutOptionsklass.
Följande kodexempel visar användningen avJsonLayoutOptionsochJsonUtilityklasser för att laddakällfil JSONoch genererarutgång CSV fil.
Exempelkod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |