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