تحويل CSV إلى JSON
تحويل CSV إلى JSON
Aspose.Cells for Python via .NET يدعم تحويل CSV إلى JSON. لهذا، يوفر الواجهة البرمجية ExportRangeToJsonOptions و JsonUtility. الفئة ExportRangeToJsonOptions توفر الخيارات لتصدير النطاق إلى JSON. الفئة ExportRangeToJsonOptions لها الخصائص التالية.
- export_as_string: يقوم بتصدير قيمة السلسلة للخلايا إلى JSON.
- has_header_row: يشير إذا كانت المدى يحتوي على صف رأسي.
- indent: يشير إلى التنسيق.
تقوم الفئة JsonUtility بتصدير JSON باستخدام خيارات التصدير المعينة باستخدام الفئة ExportRangeToJsonOptions.
يُظهر الكود العيني التالي استخدام واجهة البرمجة ExportRangeToJsonOptions وواجهة البرمجة JsonUtility لتحميل ملف CSV المصدر ويطبع الإخراج JSON في وحدة التحكم.
الكود المثالي
from aspose.cells import Workbook | |
from aspose.cells.utility import ExportRangeToJsonOptions, JsonUtility | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Source directory | |
sourceDir = RunExamples.Get_SourceDirectory() | |
# Load CSV file | |
workbook = Workbook(sourceDir + "SampleCsv.csv") | |
lastCell = workbook.worksheets[0].cells.last_cell | |
# Set ExportRangeToJsonOptions | |
options = ExportRangeToJsonOptions() | |
range = workbook.worksheets[0].cells.create_range(0, 0, lastCell.row + 1, lastCell.column + 1) | |
data = JsonUtility.export_range_to_json(range, options) | |
# Print JSON | |
print(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
}
]