Konvertieren Sie JSON in CSV
Contents
[
Hide
]
JSON in CSV konvertieren
Aspose.Cells für Python via .NET unterstützt die Konvertierung von einfachem sowie verschachteltem JSON in CSV. Dazu bietet die API die Klassen JsonLayoutOptions und JsonUtility. Die Klasse JsonLayoutOptions bietet die Optionen für das JSON-Layout wie ignore_array_title (ignoriert den Titel, wenn das Array eine Eigenschaft eines Objekts ist) oder array_as_table (verarbeitet das Array als Tabelle). Die Klasse JsonUtility verarbeitet das JSON mit den mit der Klasse JsonLayoutOptions festgelegten Layoutoptionen.
Der folgende Beispielcode zeigt die Verwendung der JsonLayoutOptions- und JsonUtility-Klassen zum Laden der Quell-JSON-Datei und erzeugt die Ausgabe-CSV-Datei.
Beispielcode
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")) |