Converti CSV in JSON con Node.js tramite C++
Convertire CSV in JSON
Aspose.Cells supporta la conversione di CSV in JSON. A tale scopo, l’API fornisce le classi ExportRangeToJsonOptions e JsonUtility. La classe ExportRangeToJsonOptions fornisce le opzioni per esportare l’intervallo in JSON. La classe ExportRangeToJsonOptions ha le seguenti proprietà.
- ExportRangeToJsonOptions.getExportAsString(): Questo esporta il valore stringa delle celle in JSON.
- ExportRangeToJsonOptions.getHasHeaderRow(): Questo indica se l’intervallo contiene una riga di intestazione.
- ExportRangeToJsonOptions.getIndent(): Indica l’indentazione.
La classe JsonUtility esporta il JSON utilizzando le opzioni di esportazione impostate con la classe ExportRangeToJsonOptions.
Il seguente esempio di codice dimostra l’uso delle classi ExportRangeToJsonOptions e JsonUtility per caricare il file CSV di origine e visualizza l’output JSON sulla console.
Codice di Esempio
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);
Output della 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
}
]