تحويل CSV إلى JSON باستخدام C++

تحويل CSV إلى JSON

يدعم Aspose.Cells تحويل CSV إلى JSON. لهذا، توفر API الفئات ExportRangeToJsonOptions و JsonUtility. توفر فئة ExportRangeToJsonOptions الخيارات لتصدير النطاق إلى JSON. تحتوي فئة ExportRangeToJsonOptions على الخصائص التالية.

  • GetExportAsString(): يقوم بتصدير قيمة السلسلة للخلايا إلى JSON.
  • GetHasHeaderRow(): يشير إلى ما إذا كان النطاق يحتوي على صف رأس.
  • GetIndent(): يشير إلى المسافة البادئة.

تقوم فئة JsonUtility بتصدير JSON باستخدام خيارات التصدير المحددة بواسطة فئة ExportRangeToJsonOptions.

يوضح المثال البرمجي التالي استخدام فئات ExportRangeToJsonOptions و JsonUtility لتحميل ملف CSV المصدر (104398879.csv) وطباعة إخراج JSON في وحدة التحكم.

الكود المثالي

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Create load options for CSV format
    LoadOptions loadOptions(LoadFormat::Csv);

    // Load CSV file
    Workbook workbook(srcDir + u"SampleCsv.csv", loadOptions);

    // Get the last cell in the worksheet
    Cell lastCell = workbook.GetWorksheets().Get(0).GetCells().GetLastCell();

    // Set JsonSaveOptions
    JsonSaveOptions jsonSaveOptions;

    // Create a range from the first cell to the last cell
    Range range = workbook.GetWorksheets().Get(0).GetCells().CreateRange(0, 0, lastCell.GetRow() + 1, lastCell.GetColumn() + 1);

    // Export the range to JSON
    U16String data = JsonUtility::ExportRangeToJson(range, jsonSaveOptions);

    // Print JSON
    std::cout << data.ToUtf8() << std::endl;

    Aspose::Cells::Cleanup();
}

مخرجات الوحدة

[
{
"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
}
]