The requested page is not available for the requested platform. You are viewing the content for Default platform.

Bind a Report to JSON Data (Runtime Sample)

This topic describes how to bind a report to JSON data at runtime.

Create an JsonDataSource Object

Use the JsonDataSource component to bind a report to JSON-formatted data.

using DevExpress.DataAccess.Json;
using DevExpress.XtraReports.UI;
//...
private JsonDataSource CreateDataSourceFromWeb()
{
    var jsonDataSource = new JsonDataSource();
    //Specify the data source location
    jsonDataSource.JsonSource = new UriJsonSource(new Uri("http://northwind.servicestack.net/customers.json"));

    //Retrieve data from the JSON data source to the Report Designer's Field List
    jsonDataSource.Fill();
    return jsonDataSource;
}
Tip

You can also provide the created JsonDataSource object with authentication parameters to access the specified Web Endpoint. Refer to the Provide Authentication to Access JSON Data topic for more information.

Define JSON Data Scheme

You can specify from which JSON nodes to retrieve data. Define a scheme and assign it to the JsonDataSource's Schema property.

using DevExpress.DataAccess.Json;
using DevExpress.XtraReports.UI;
//...
private JsonDataSource CreateDataSourceFromWeb()
{
    var jsonDataSource = new JsonDataSource();
    //Specify the data source location
    jsonDataSource.JsonSource = new UriJsonSource(new Uri("http://northwind.servicestack.net/customers.json"));

    // Define the Customers node's scheme to retrieve only required child nodes and fields
    var customers = new JsonSchemaNode() {NodeType=JsonNodeType.Array, Name="Customers", Selected=true };
    customers.AddChildren(new[] {
        new JsonSchemaNode(new JsonNode("CustomerID", true, JsonNodeType.Property, typeof(string))) { DisplayName = "Customer ID" },
        new JsonSchemaNode() {
            Name =  "CompanyName",
            Selected = true,
            NodeType = JsonNodeType.Property,
            Type = typeof(string)
        },
        new JsonSchemaNode(new JsonNode("ContactTitle", true, JsonNodeType.Property, typeof(string))),
        //Set the JsonNode.Selected to false not to retrieve the Address field
        new JsonSchemaNode(new JsonNode("Address", false, JsonNodeType.Property, typeof(string)))
    });


    // Create a root schema node
    var root = new JsonSchemaNode();
    root.NodeType = JsonNodeType.Object;
    // Add the defined Customers schema node to the root node
    root.AddChildren(customers);
    // Specify the JsonDataSource's Scheme property to retrieve data only from the Customers node
    jsonDataSource.Schema = root;

    // Retrieve data from the JSON data source to the Report Designer's Field List
    jsonDataSource.Fill();
    return jsonDataSource;
}

Bind the Report to the JsonDataSource

The following code snippet assigns the JsonDataSource object to the report's DataSource property:

using DevExpress.XtraReports.UI;
//...
private XtraReport CreateReport() {
    XtraReport report = new XtraReport {
        Bands = {
            new DetailBand {
                Controls = {
                    new XRLabel {
                        ExpressionBindings = {
                            new ExpressionBinding("BeforePrint", "Text", "[CompanyName]")
                        },
                        WidthF = 300
                    }
                },
                HeightF = 50
            }
        },
        DataSource = CreateDataSource()
    };
    return report;
}

Install the Newtonsoft.json Package

The JsonDataSource component uses the open source Newtonsoft.Json library to provide JSON data at runtime. Install the Newtonsoft.Json package if your application does not reference this library.

Preview and Publish the Report

You can now call the CreateReport() function and invoke the End-User Designer with the created report. Refer to the following topics for instructions on how to do this in different platforms:

JSON-Runtime

See Also