将 JSON 转换为 CSV
Contents
[
Hide
]
Aspose.Cells 支持将简单和嵌套的 JSON 转换为 CSV。为此,API 提供JsonLayoutOptions和Json工具类。这JsonLayoutOptions类提供 JSON 布局的选项,例如忽略数组标题(如果数组是对象的属性,则忽略标题)或ArrayAsTable(将数组作为表格处理)。这Json工具类使用设置的布局选项处理 JSONJsonLayoutOptions班级。
下面的代码示例演示了使用JsonLayoutOptions和Json工具加载的类源文件 JSON并生成输出 CSV 文件.
示例代码
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"); |