Convertir CSV en JSON avec Node.js via C++

Convertir CSV en JSON

Aspose.Cells prend en charge la conversion de CSV en JSON. Pour cela, l’API fournit les classes ExportRangeToJsonOptions et JsonUtility. La classe ExportRangeToJsonOptions fournit les options pour exporter la plage vers JSON. La classe ExportRangeToJsonOptions a les propriétés suivantes.

La classe JsonUtility exporte le JSON en utilisant les options d’export définies avec la classe ExportRangeToJsonOptions.

Le code d’exemple suivant démontre l’utilisation des classes ExportRangeToJsonOptions et JsonUtility pour charger le fichier CSV source et afficher la sortie JSON dans la console.

Code d’exemple

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

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

const loadOptions = new AsposeCells.LoadOptions(AsposeCells.LoadFormat.Csv);
// Load CSV file
const filePath = path.join(sourceDir, "SampleCsv.csv");
const workbook = new AsposeCells.Workbook(filePath, loadOptions);
const lastCell = workbook.getWorksheets().get(0).getCells().getLastCell();

// Set JsonSaveOptions
const jsonSaveOptions = new AsposeCells.JsonSaveOptions();
const range = workbook.getWorksheets().get(0).getCells().createRange(0, 0, lastCell.getRow() + 1, lastCell.getColumn() + 1);
const data = AsposeCells.JsonUtility.exportRangeToJson(range, jsonSaveOptions);

// Print JSON
console.log(data);

Sortie console

[
{
"id": 1,
"language": "Java",
"edition": "third",
"author": "Herbert Schildt",
"streetAddress": 126,
"city": "San Jone",
"state": "CA",
"postalCode": 394221
},
{
"id": 2,
"language": "C++",
"edition": "second",
"author": "EAAAA",
"streetAddress": 126,
"city": "San Jone",
"state": "CA",
"postalCode": 394221
},
{
"id": 3,
"language": ".Net",
"edition": "second",
"author": "E.Balagurusamy",
"streetAddress": 126,
"city": "San Jone",
"state": "CA",
"postalCode": 394221
}
]