将 JSON 转换为 CSV
Contents
[
Hide
]
将 JSON 转换为 CSV
Aspose.Cells 支持将简单和嵌套的 JSON 转换为 CSV。为此,API 提供**JsonLayoutOptions**和**JsonUtility**类。这**JsonLayoutOptions**类提供 JSON 布局的选项,例如**IgnoreArrayTitle**(如果数组是对象的属性,则忽略标题)或**ArrayAsTable**(将数组作为表格处理)。这**JsonUtility**类使用设置的布局选项处理 JSON**JsonLayoutOptions**班级。
下面的代码示例演示了使用**JsonLayoutOptions**和**JsonUtility**加载的类源文件 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-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Read JSON file | |
string str = File.ReadAllText(sourceDir + "SampleJson.json"); | |
// Create empty workbook | |
Workbook workbook = new Workbook(); | |
// Get Cells | |
Cells cells = workbook.Worksheets[0].Cells; | |
// Set JsonLayoutOptions | |
JsonLayoutOptions importOptions = new JsonLayoutOptions(); | |
importOptions.ConvertNumericOrDate = true; | |
importOptions.ArrayAsTable = true; | |
importOptions.IgnoreTitle = true; | |
JsonUtility.ImportData(str, cells, 0, 0, importOptions); | |
// Save Workbook | |
workbook.Save(outputDir + @"SampleJson_out.csv"); |