HTML to JSON C#

HTML pages load in browsers and showcase all kinds of data. Sometimes you might want to extract data from an HTML file and convert it into JSON, a lightweight, human-readable format that’s widely used for data exchange between a server and a client. In this article, I’ll walk you through how to convert HTML to JSON using C#.

The topics we’ll cover are:

  1. HTML to JSON Converter C# API
  2. Convert HTML to JSON

HTML to JSON Converter C# API

To convert HTML to JSON, we’re using the powerful and easy-to-use Aspose.Cells JSON Converter for .NET.

The Workbook class represents a spreadsheet and can load HTML files. Each workbook may contain several worksheets, and you can even combine or copy data between them. To export a specific range as JSON, the API uses the ExportRangeToJsonOptions class to set your export options. You then create a range from cells with the CreateRange() method provided by the Cells class. Finally, you call the JsonUtility.ExportRangeToJson() method to generate your JSON string.

If you want to get started, you can either download the DLL or install it directly from NuGet:

PM> Install-Package Aspose.Cells

Convert HTML to JSON in C#

The process to convert HTML into JSON with C# is straightforward:

  1. Load the HTML file: Start by opening the HTML file using the Workbook class.
  2. Set your export options: Create an instance of the ExportRangeToJsonOptions class.
  3. Define a cell range: Use the CreateRange() method to create a range of cells.
  4. Export to JSON: Convert the selected range into a JSON string using JsonUtility.ExportRangeToJson().
  5. Save your JSON data: Write the resulting JSON string to a file using File.WriteAllText().

The code example below demonstrates how to convert HTML to JSON using C#:

Convert HTML to JSON in C#

Get a Free License

You can try the library without any limitations by obtaining a free temporary license.

Conclusion

In this article, we explored how to extract data from an HTML file and convert it into JSON using C#. We covered loading the file, configuring the export settings, creating a cell range, and finally generating and saving the JSON string. For more details about the API, check out the documentation or ask questions on the forum.

To further enhance your workflow, you might also consider learning how to C# Export HTML to Excel or how to Convert HTML File to Excel C# for additional data handling capabilities.

More in this category