HTML webpages are rendered in web browsers and can display information/data. We can easily export data from an HTML file to JSON format programmatically. JSON uses human-readable text to store and transmit data. It is commonly used to send data from the server to the client. In this article, we will learn how to convert HTML to JSON using C#.
The following topics shall be covered in this article:
HTML to JSON Converter C# API
For converting HTML to JSON, we will be using the Aspose.Cells for .NET API. It is a feature-rich, powerful and easy-to-use API to work with Microsoft Excel-associated worksheets within the .NET applications. It enables us to generate, modify, convert, render and print spreadsheets.
The Workbook class of the API represents an Excel spreadsheet. Each spreadsheet can contain multiple worksheets. It allows the opening and saving of native Excel files. It also provides methods to copy data from other Workbooks, combine two Workbooks, and protect the Excel spreadsheet. The ExportRangeToJsonOptions class of the API indicates the options that export range to JSON. The CreateRange() method of the Cells class, creates a Range object from a range of cells. The API provides JsonUtility.ExportRangeToJson() method to export provided range in a JSON string.
PM> Install-Package Aspose.Cells
Convert HTML to JSON in C#
We can export data from HTML to JSON by following the steps given below:
- Firstly, load the HTML file using the Workbook class.
- Next, create an instance of the ExportRangeToJsonOptions class.
- Then, call the CreateRange() method to create a range of cells.
- After that, call the JsonUtility.ExportRangeToJson() method to export JSON string.
- Finally, write the exported string to the JSON file using the File.WriteAllText() method.
The code example given below shows how to convert HTML to JSON using C#.
Get a Free License
You can get a free temporary license to try the library without evaluation limitations.
In this article, we have learned how to export data from an HTML file to JSON in C#. We have also seen how to create a range of cells for a specific worksheet programmatically. Besides, you can learn more about Aspose.Cells for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.