Register Json data in FastReport.NET

2015-03-23

If you need to register Json as data source in FastReport.NET, this can be done through the registration of business objects.
For parsing json you need to describe the data scheme in the C# classes, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 public class Product
 {
 public string Name { get; set; }
 public double UnitPrice { get; set; }
 }
 
 public class Category
 {
 public string Name { get; set; }
 public string Description { get; set; }
 public List<Product> Products { get; set; }
 }
 
 string json = "..."; // your json
 
 List<Category> categories = JsonConvert.DeserializeObject<List<Category>>(json); // using the Newtonsoft.Json library for deserialization
 
 Report report = new Report();
 report.Load(@"C:\report.frx");
 report.RegisterData(categories, "Categories");

If you do not know the scheme or if it is changing during the work of the program, it can be generated dynamically. For this we use the library JSON C# Class Generator, you can download it from https://jsonclassgenerator.codeplex.com.
Code to generate C# classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 string json = "..."; // your json
 
 if (json.StartsWith("[")) // if json is an array, then it would be better if we name it
 {
 json = "{\"Data\":" + json + "}"; // Under this name the data will be displayed in designer, so you can add multiple data sources
 }
 
 JsonClassGenerator gen = new JsonClassGenerator()
 {
 Example = json,
 UseProperties = true,
 Namespace = "__JSON__",
 MainClass = "__JSON__",
 };
 
 string source = "";
 using (StringWriter sw = new StringWriter())
 {
 gen.OutputStream = sw;
 gen.GenerateClasses();
 sw.Flush();
 source = sw.ToString();
 }
As a result we obtain the generated classes in a string. Now we need to access them from the code; in order to do this compile them using CSharpCodeProvider and get Type:
1
2
3
4
5
6
7
8
9
10
 Type type = null;
 using (CSharpCodeProvider compiler = new CSharpCodeProvider())
 {
 CompilerParameters parameters = new CompilerParameters()
 {
 GenerateInMemory = true,
 };
 CompilerResults results = compiler.CompileAssemblyFromSource(parameters, source);
 type = results.CompiledAssembly.GetType("__JSON__.__JSON__");
 }

Note: This example shows a compilation in the current AppDomain. This means that it can not be unloaded from memory, and each compilation will eat off memory. To avoid this, you can use CSharpCodeProvider in another AppDomain.

Deserialize json using the generated classes. The easiest way to do this is to use the library Newtonsoft.Json; You can get it through NuGet with the command "Install-Package Newtonsoft.Json".

1
 object obj = JsonConvert.DeserializeObject(json, type);
Now we have objects, so let's register them in FastReport.NET:
1
2
3
4
5
6
7
8
9
 PropertyInfo[] properties = type.GetProperties();
 
 Report report = new Report();
 report.Load(@"C:\report.frx");
 
 foreach (var prop in properties)
 {
 report.RegisterData((IList)prop.GetValue(obj, null), prop.Name);
 }

So now you can run designer and see the results.

Data

.NET FastReport .NET FastReport
February 20, 2025

Using Dot Matrix Printers for Printing Receipts

This article will explore how to integrate a dot matrix printer into the process of creating and outputting reports using FastReport .NET.
February 17, 2025

Adding New Users to FastReport Cloud

In the Business edition of the FastReport Cloud service, you can add users to a single workspace for simultaneous collaboration.
February 10, 2025

How to Try FastReport .NET WEB Before Purchase

By testing the WEB pack before purchasing, you can make an informed choice about whether FastReport is suitable for you.NET for your tasks.
Fast Reports
  • 800-985-8986 (English, US)
  • +31 97 01025-8466 (English, EU)
  • +49 30 56837-3928 (German, DE)
  • +55 19 98147-8148 (Portuguese, BR)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2025 Fast Reports Inc.