Convertir JSON en CSV
Convertir JSON en CSV
Aspose.Cells prend en charge la conversion de JSON simple ainsi que de JSON imbriqué en CSV. Pour cela, l’API fournit les classes JsonLayoutOptions et JsonUtility. La classe JsonLayoutOptions fournit les options pour la disposition du JSON comme IgnoreArrayTitle (ignore le titre si le tableau est une propriété d’un objet) ou ArrayAsTable (traite le tableau comme une table). La classe JsonUtility traite le JSON en utilisant les options de disposition définies avec la classe JsonLayoutOptions.
Le code d’exemple suivant démontre l’utilisation des classes JsonLayoutOptions et JsonUtility pour charger le fichier JSON source et générer le fichier CSV en sortie.
Code d’exemple
// 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(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Read JSON file | |
string str = File.ReadAllText(sourceDir + "SampleJson.json"); | |
// Create empty workbook | |
Workbook workbook = new Workbook(); | |
// Get Cells | |
Cells cells = workbook.Worksheets[0].Cells; | |
// Set JsonLayoutOptions | |
JsonLayoutOptions importOptions = new JsonLayoutOptions(); | |
importOptions.ConvertNumericOrDate = true; | |
importOptions.ArrayAsTable = true; | |
importOptions.IgnoreTitle = true; | |
JsonUtility.ImportData(str, cells, 0, 0, importOptions); | |
// Save Workbook | |
workbook.Save(outputDir + @"SampleJson_out.csv"); |