Converti JSON in CSV con Node.js tramite C++

Convertire JSON in CSV

Aspose.Cells supporta la conversione di JSON semplice e annidato in CSV. Per questo, l’API fornisce le classi JsonLayoutOptions e JsonUtility. La classe JsonLayoutOptions fornisce le opzioni per il layout JSON come JsonLayoutOptions.getArrayAsTable() (elabora l’array come tabella). La classe JsonUtility elabora il JSON utilizzando le opzioni di layout impostate con la classe JsonLayoutOptions.

Il seguente esempio di codice dimostra l’uso delle classi JsonLayoutOptions e JsonUtility per caricare il file JSON sorgente e genera il file CSV di output.

Codice di Esempio

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");