Converti CSV in JSON

Converti CSV in JSON

Aspose.Cells supporta la conversione da CSV a JSON. Per questo, API fornisce**ExportRangeToJsonOptions**e**JsonUtility** classi. Il**ExportRangeToJsonOptions**class fornisce le opzioni per l’esportazione dell’intervallo a JSON. Il**ExportRangeToJsonOptions**class ha le seguenti proprietà.

Il**JsonUtility**class esporta lo JSON utilizzando le opzioni di esportazione impostate con il file**ExportRangeToJsonOptions**classe.

L’esempio di codice seguente illustra l’utilizzo di**ExportRangeToJsonOptions**e**JsonUtility** classi per caricare il filefonte CSV filee stampa l’output JSON nella console.

Codice d’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);

Uscita 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
}
]