Skip to main content
A newer version of this page is available. .

JsonDataSource Class

The data source that extracts data stored in JSON format.

Namespace: DevExpress.DataAccess.Json

Assembly: DevExpress.DataAccess.v19.1.dll

Declaration

[XRDesigner("DevExpress.DataAccess.UI.Design.XRJsonDataSourceDesigner,DevExpress.DataAccess.v19.1.UI, Version=19.1.99.0, Culture=neutral, PublicKeyToken=c38a27d2243c2672", typeof(IDesigner))]
[ToolboxSvgImage("DevExpress.DataAccess.Images.JsonDataSource.svg,DevExpress.DataAccess.v19.1, Version=19.1.99.0, Culture=neutral, PublicKeyToken=c38a27d2243c2672")]
[ToolboxBitmap(typeof(ResFinder), "Bitmaps256.JsonDataSource.bmp")]
public class JsonDataSource :
    DataComponentBase,
    ITypedList,
    IListAdapter,
    IList,
    ICollection,
    IEnumerable,
    IBindingList

Remarks

The JsonDataSource object allows you to extract JSON data from a Web-service endpoint, text file or JSON string.

Note

In this version, the JsonDataSource is supported in Reporting only.

You can create the JsonDataSource object both at design time and runtime.

Design Time

Use the Data Source Wizard that the target control (e.g., the Report Designer in the images below) provides to create a JsonDataSource object. The wizard’s pages allow you to specify the JSON data location and select required JSON nodes.

After you finish the wizard, it creates the JsonDataSource object. In the Properties window, you can change the JsonDataSource‘s property values.

Refer to the Bind a Report to JSON Data topic for more information on how to create and configure the JsonDataSource object in the Report Designer at design time.

Runtime

Do the following to configure the JsonDataSource object at runtime:

  1. Use the JsonDatasource‘s JsonSource property to specify the JSON data location.

  2. (Optionally) Use the Schema property to specify from which JSON nodes to retrieve data.

  3. Use the Fill() method to fill to the Report Designer’s Field List.

The code sample below illustrates how to use JSON data from the Web.

using DevExpress.DataAccess.Json;
using DevExpress.XtraReports.UI;
// ...
public static 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;
}

The code sample below illustrates how to use JSON data from a file.

using DevExpress.DataAccess.Json;
using DevExpress.XtraReports.UI;
public static JsonDataSource CreateDataSourceFromFile() {
    var jsonDataSource = new JsonDataSource();
    // Specify the a JSON file's name
    Uri fileUri = new Uri("customers.json", UriKind.RelativeOrAbsolute);
    jsonDataSource.JsonSource = new UriJsonSource(fileUri);
    // Retrieve data from the JSON data source to the Report Designer's Field List
    jsonDataSource.Fill();
    return jsonDataSource;
}

The code sample below illustrates how to use JSON data from a string variable.

using DevExpress.DataAccess.Json;
using DevExpress.XtraReports.UI;
public static JsonDataSource CreateDataSourceFromText() {
    var jsonDataSource = new JsonDataSource();

    // Specify a string with JSON content
    string json = "{\"Customers\":[{\"Id\":\"ALFKI\",\"CompanyName\":\"Alfreds Futterkiste\",\"ContactName\":\"Maria Anders\",\"ContactTitle\":\"Sales Representative\",\"Address\":\"Obere Str. 57\",\"City\":\"Berlin\",\"PostalCode\":\"12209\",\"Country\":\"Germany\",\"Phone\":\"030-0074321\",\"Fax\":\"030-0076545\"}],\"ResponseStatus\":{}}";

    // Specify the object that retrieves JSON data
    jsonDataSource.JsonSource = new CustomJsonSource(json);
    // Retrieve data from the JSON data source to the Report Designer's Field List
    jsonDataSource.Fill();
    return jsonDataSource;
}

Refer to the Bind a Report to JSON Data (Runtime Sample) topic for more information on how to create and configure the JsonDataSource object and then bind it to a report at runtime. 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 (Runtime Sample) topic for more information.

Install the Newtonsoft.json Package

The JsonDataSource object uses the open source Newtonsoft.Json library. Install the Newtonsoft.Json package if your application does not reference this library.

Implements

Inheritance

Object
MarshalByRefObject
Component
DevExpress.DataAccess.DataComponentBase
JsonDataSource
See Also