将JSON转换为CSV
Contents
[
Hide
]
Aspose.Cells支持将简单和嵌套的JSON转换为CSV。为此,API提供了JsonLayoutOptions和JsonUtility类。JsonLayoutOptions类提供了JSON布局的选项,比如IgnoreArrayTitle(如果数组是对象的属性,则忽略标题)或者ArrayAsTable(将数组处理为表)。JsonUtility类使用与JsonLayoutOptions类设置的布局选项处理JSON。
以下代码示例演示了如何使用JsonLayoutOptions和JsonUtility类加载source JSON file并生成output CSV file。
示例代码
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"); |