Converti JSON in CSV con C++

Convertire JSON in CSV

Aspose.Cells supporta la conversione di JSON semplice e annidato in CSV. Per questo, l’API fornisce le classi JsonLayoutOptions e JsonUtility. La classe JsonLayoutOptions offre le opzioni per il layout JSON come SetIgnoreTitle (ignora il titolo se l’array è una proprietà di un oggetto) o GetArrayAsTable() (elabora l’array come una tabella). La classe JsonUtility elabora il JSON usando le opzioni di layout impostate con la classe JsonLayoutOptions.

Il seguente esempio di codice dimostra l’uso delle classi JsonLayoutOptions e JsonUtility per caricare il file JSON sorgente e genera il file CSV di output.

Codice di Esempio

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

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

    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    U16String jsonFilePath = srcDir + u"SampleJson.json";
    U16String jsonData;
    std::ifstream jsonFile(jsonFilePath.ToUtf8().c_str());
    if (jsonFile.is_open())
    {
        std::stringstream buffer;
        buffer << jsonFile.rdbuf();
        jsonData = U16String(buffer.str().c_str());
        jsonFile.close();
    }
    else
    {
        std::cerr << "Failed to open JSON file: " << jsonFilePath.ToUtf8().c_str() << std::endl;
        return -1;
    }

    Workbook workbook;
    Cells cells = workbook.GetWorksheets().Get(0).GetCells();

    JsonLayoutOptions importOptions;
    importOptions.SetConvertNumericOrDate(true);
    importOptions.SetArrayAsTable(true);
    importOptions.SetIgnoreTitle(true);

    JsonUtility::ImportData(jsonData, cells, 0, 0, importOptions);

    U16String outputFilePath = outDir + u"SampleJson_out.csv";
    workbook.Save(outputFilePath);

    std::cout << "JSON data imported and workbook saved successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}