Converti CSV in JSON
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à.
- 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 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Csv); | |
// Load CSV file | |
Workbook workbook = new Workbook(sourceDir + "SampleCsv.csv", loadOptions); | |
Cell lastCell = workbook.Worksheets[0].Cells.LastCell; | |
// Set JsonSaveOptions | |
JsonSaveOptions jsonSaveOptions = new JsonSaveOptions(); | |
Range range = workbook.Worksheets[0].Cells.CreateRange(0, 0, lastCell.Row + 1, lastCell.Column + 1); | |
string data = JsonUtility.ExportRangeToJson(range, jsonSaveOptions); | |
// Print JSON | |
Console.WriteLine(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
}
]