Converti CSV in JSON
Convertire CSV in JSON
Aspose.Cells supporta la conversione da CSV a JSON. A tal fine, 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à.
- ExportAsString: Questo esporta il valore stringa delle celle in JSON.
- HasHeaderRow: Questo indica se l’intervallo contiene una riga di intestazione.
- Indent: Indica l’indentazione.
La classe JsonUtility esporta il JSON utilizzando le opzioni di esportazione impostate con la classe ExportRangeToJsonOptions.
Il seguente codice di esempio dimostra l’uso delle classi ExportRangeToJsonOptions e JsonUtility per caricare il file CSV di origine e stampa l’output JSON nella console.
Codice di 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); |
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
}
]