将JSON转换为CSV
Contents
[
Hide
]
将JSON转换为CSV
Aspose.Cells for Python via .NET 可以将简单和嵌套的 JSON 转换为 CSV。为此,API 提供了 JsonLayoutOptions 和 JsonUtility 类。JsonLayoutOptions 类提供了 JSON 布局的选项,如 ignore_array_title(如果数组是对象的属性,则忽略标题)或 array_as_table(将数组处理为表)。JsonUtility 类使用使用与 JsonLayoutOptions 类设置的布局选项处理 JSON。
以下代码示例演示了使用 JsonLayoutOptions 和 JsonUtility 类加载 源 JSON 文件 并生成 输出 CSV 文件。
示例代码
This file contains hidden or 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
from aspose.cells import Workbook | |
from aspose.cells.utility import JsonLayoutOptions, JsonUtility | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Source directory | |
sourceDir = RunExamples.Get_SourceDirectory() | |
# Output directory | |
outputDir = RunExamples.Get_OutputDirectory() | |
# Read JSON file | |
str = open(sourceDir + "SampleJson.json", "r").read() | |
# Create empty workbook | |
workbook = Workbook() | |
# Get Cells | |
cells = workbook.worksheets[0].cells | |
# Set JsonLayoutOptions | |
importOptions = JsonLayoutOptions() | |
importOptions.convert_numeric_or_date = True | |
importOptions.array_as_table = True | |
importOptions.ignore_array_title = True | |
importOptions.ignore_object_title = True | |
JsonUtility.import_data(str, cells, 0, 0, importOptions) | |
# Save Workbook | |
workbook.save(outputDir + str(r"SampleJson_out.csv")) |