JSONをCSVに変換する

Aspose.Cellsは、単純なJSONだけでなく、ネストされたJSONをCSVに変換することをサポートしています。このため、APIはJsonLayoutOptionsおよびJsonUtilityクラスを提供します。JsonLayoutOptionsクラスは、JSONレイアウトに関するオプションを提供します。たとえば、IgnoreArrayTitle(オブジェクトのプロパティの配列の場合、タイトルを無視します)またはArrayAsTable(テーブルとして配列を処理します)。JsonUtilityクラスは、JsonLayoutOptionsクラスで設定されたレイアウトオプションを使用してJSONを処理します。

次のコードサンプルは、JsonLayoutOptionsおよびJsonUtilityクラスを使用して、source JSON fileをロードし、output CSV fileを生成する方法を示しています。

サンプルコード

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Source directory
String sourceDir = Utils.Get_SourceDirectory();
//Output directory
String outputDir = Utils.Get_OutputDirectory();
// Read JSON file
String str = new String(Files.readAllBytes(Paths.get(sourceDir + "SampleJson.json")));
// Create empty workbook
Workbook workbook = new Workbook();
// Get Cells
Cells cells = workbook.getWorksheets().get(0).getCells();
// Set JsonLayoutOptions
JsonLayoutOptions importOptions = new JsonLayoutOptions();
importOptions.setConvertNumericOrDate(true);
importOptions.setArrayAsTable(true);
importOptions.setIgnoreArrayTitle(true);
importOptions.setIgnoreObjectTitle(true);
JsonUtility.importData(str, cells, 0, 0, importOptions);
// Save Workbook
workbook.save(outputDir + "SampleJson_out.csv");