تحويل CSV إلى JSON
تحويل CSV إلى JSON
تدعم Aspose.Cells تحويل CSV إلى JSON. لهذا الغرض، توفر واجهة البرمجة ExportRangeToJsonOptions وواجهة البرمجة JsonUtility. توفر واجهة البرمجة ExportRangeToJsonOptions خيارات لتصدير المجال إلى JSON. تحتوي واجهة البرمجة ExportRangeToJsonOptions على الخصائص التالية.
- ExportAsString: يقوم بتصدير قيمة السلسلة للخلايا إلى JSON.
- HasHeaderRow: يشير إذا كانت المدى يحتوي على صف رأسي.
- Indent: يشير إلى التنسيق.
تقوم الفئة JsonUtility بتصدير JSON باستخدام خيارات التصدير المعينة باستخدام الفئة ExportRangeToJsonOptions.
يُظهر الكود العيني التالي استخدام واجهة البرمجة ExportRangeToJsonOptions وواجهة البرمجة JsonUtility لتحميل ملف CSV المصدر ويطبع الإخراج JSON في وحدة التحكم.
الكود المثالي
// 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); |
مخرجات الوحدة
[
{
"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
}
]