Konvertieren Sie JSON nach CSV mit Node.js via C++

JSON in CSV konvertieren

Aspose.Cells unterstützt die Konvertierung von einfachem und verschachteltem JSON in CSV. Dafür bietet die API die Klassen JsonLayoutOptions und JsonUtility. Die JsonLayoutOptions-Klasse bietet Optionen für das JSON-Layout wie JsonLayoutOptions.getArrayAsTable() (verarbeitet das Array als Tabelle). Die JsonUtility-Klasse verarbeitet JSON unter Verwendung der mit der JsonLayoutOptions-Klasse gesetzten Layout-Optionen.

Das folgende Codebeispiel demonstriert die Verwendung der Klassen JsonLayoutOptions und JsonUtility, um die Quelldatei JSON zu laden und die Ausgabedatei CSV zu generieren.

Beispielcode

const fs = require("fs");
const path = require("path");
const AsposeCells = require("aspose.cells.node");

// Source directory
const sourceDir = path.join(__dirname, "data");

// Output directory
const outputDir = path.join(__dirname, "output");


// Create sample JSON if missing
const jsonPath = path.join(sourceDir, "SampleJson.json");

// Read JSON file
const str = fs.readFileSync(jsonPath, "utf-8");

// Create empty workbook
const workbook = new AsposeCells.Workbook();

// Get Cells
const cells = workbook.getWorksheets().get(0).getCells();

// Set JsonLayoutOptions
const importOptions = new AsposeCells.JsonLayoutOptions();
importOptions.setConvertNumericOrDate(true);
importOptions.setArrayAsTable(true);
importOptions.setIgnoreTitle(true);
AsposeCells.JsonUtility.importData(str, cells, 0, 0, importOptions);

// Save Workbook
workbook.save(outputDir + "SampleJson_out.csv");