Converti CSV in JSON

Converti CSV in JSON

Aspose.Cells supporta la conversione da CSV a JSON. Per questo, API fornisceExportRangeToJsonOptionseJsonUtilityclassi. IlExportRangeToJsonOptionsclass fornisce le opzioni per l’esportazione dell’intervallo a JSON. IlExportRangeToJsonOptionsclass ha le seguenti proprietà.

IlJsonUtilityclass esporta lo JSON utilizzando le opzioni di esportazione impostate con il fileExportRangeToJsonOptionsclasse.

L’esempio di codice seguente illustra l’utilizzo diExportRangeToJsonOptionseJsonUtilityclassi per caricare il filefonte CSV filee stampa ilJSON uscita nella console.

Codice d’esempio

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Source directory
String sourceDir = Utils.Get_SourceDirectory();
LoadOptions loadOptions = new LoadOptions(LoadFormat.CSV);
// Load CSV file
Workbook workbook = new Workbook(sourceDir + "SampleCsv.csv", loadOptions);
Cell lastCell = workbook.getWorksheets().get(0).getCells().getLastCell();
// Set ExportRangeToJsonOptions
ExportRangeToJsonOptions options = new ExportRangeToJsonOptions();
Range range = workbook.getWorksheets().get(0).getCells().createRange(0, 0, lastCell.getRow() + 1, lastCell.getColumn() + 1);
String data = JsonUtility.exportRangeToJson(range, options);
// Print JSON
System.out.println(data);

Uscita console

[ { “id”: 1, “language”: “Java”, “edition”: “three”, “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”, " stato": “CA”, “codicepostale”: 394221 } ]