将 CSV 转换为 JSON
Contents
[
Hide
]
将 CSV 转换为 JSON
Aspose.Cells 支持将 CSV 转换为 JSON。为此,API 提供**ExportRangeToJsonOptions**和**JsonUtility**类。这**ExportRangeToJsonOptions**类提供了将范围导出到 JSON 的选项。**ExportRangeToJsonOptions**类具有以下属性。
- ExportAsString:这会将单元格的字符串值导出为 JSON。
- HasHeaderRow:这表示范围是否包含标题行。
- **缩进**表示缩进。
这**JsonUtility**类使用设置的导出选项导出 JSON**ExportRangeToJsonOptions**班级。
下面的代码示例演示了使用**ExportRangeToJsonOptions**和**JsonUtility**加载的类源文件 CSV并在控制台中打印 JSON 输出。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Csv); | |
// Load CSV file | |
Workbook workbook = new Workbook(sourceDir + "SampleCsv.csv", loadOptions); | |
Cell lastCell = workbook.Worksheets[0].Cells.LastCell; | |
// Set JsonSaveOptions | |
JsonSaveOptions jsonSaveOptions = new JsonSaveOptions(); | |
Range range = workbook.Worksheets[0].Cells.CreateRange(0, 0, lastCell.Row + 1, lastCell.Column + 1); | |
string data = JsonUtility.ExportRangeToJson(range, jsonSaveOptions); | |
// Print JSON | |
Console.WriteLine(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
}
]