JsonDataSource Class

The data source that retrieves and provides JSON data.

Namespace: DevExpress.DataAccess.Json

Assembly: DevExpress.DataAccess.v20.1.dll

Declaration

[XRDesigner("DevExpress.DataAccess.UI.Design.XRJsonDataSourceDesigner,DevExpress.DataAccess.v20.1.UI, Version=20.1.99.0, Culture=neutral, PublicKeyToken=c38a27d2243c2672", typeof(IDesigner))]
[ToolboxSvgImage("DevExpress.DataAccess.Images.JsonDataSource.svg,DevExpress.DataAccess.v20.1, Version=20.1.99.0, Culture=neutral, PublicKeyToken=c38a27d2243c2672")]
[ToolboxBitmap(typeof(ResFinder), "Bitmaps256.JsonDataSource.bmp")]
public class JsonDataSource :
    DataComponentBase,
    ITypedList,
    IListAdapter,
    IList,
    ICollection,
    IEnumerable,
    IBindingList,
    ISupportFillAsync,
    IListAdapterAsync
<XRDesigner("DevExpress.DataAccess.UI.Design.XRJsonDataSourceDesigner,DevExpress.DataAccess.v20.1.UI, Version=20.1.99.0, Culture=neutral, PublicKeyToken=c38a27d2243c2672", GetType(IDesigner))>
<ToolboxSvgImage("DevExpress.DataAccess.Images.JsonDataSource.svg,DevExpress.DataAccess.v20.1, Version=20.1.99.0, Culture=neutral, PublicKeyToken=c38a27d2243c2672")>
<ToolboxBitmap(GetType(ResFinder), "Bitmaps256.JsonDataSource.bmp")>
Public Class JsonDataSource
    Inherits DataComponentBase
    Implements ITypedList,
               IListAdapter,
               IList,
               ICollection,
               IEnumerable,
               IBindingList,
               ISupportFillAsync,
               IListAdapterAsync

Remarks

The JsonDataSource class allows you to obtain JSON data from a web service endpoint, text file, or JSON string. Use this class in Reports or WinForms data-aware controls (for instance, Data Grid) to provide JSON data to them.

NOTE

In Dashboards, use the DashboardJsonDataSource component instead.

You can create a JsonDataSource object at design time or at runtime.

Design Time

Use one of the options below to create a JsonDataSource.

  • Add the JsonDataSource** component from the Toolbox (for WinForms controls)

    See the Bind to JSON Data at Design Time section for more information.

  • Invoke the Data Source Configuration Wizard (for WinForms controls)

    See the Bind WinForms Controls to JSON Data topic for more information.

  • Invoke the Report Data Source Wizard (for XtraReports)

    The Report Data Source Wizard allows you to specify the JSON data location, configure parameters, and customize the schema.

    See the Bind a Report to JSON Data topic for more information.

After a JsonDataSource class instance is created, you can access and customize this instance in the Properties window.

Runtime

Follow the steps below to configure a JsonDataSource class instance:

  1. Use the JsonDataSource.JsonSource property to specify the JSON data location.

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

  3. Use the JsonDataSource.Fill or JsonDataSource.FillAsync method to fill the JsonDataSource with data. Handle the FillError event (or catch the JsonDataSourceException exception) to ensure that the data source has been populated successfully.

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

using DevExpress.DataAccess.Json;
// ...
public static JsonDataSource CreateDataSourceFromWeb() {
    var jsonDataSource = new JsonDataSource();
    // Specify the endpoint.
    jsonDataSource.JsonSource = new UriJsonSource(new Uri("http://northwind.servicestack.net/customers.json"));
    // Populate the data source with data.
    jsonDataSource.Fill();
    return jsonDataSource;
}

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

using DevExpress.DataAccess.Json;
// ...
public static JsonDataSource CreateDataSourceFromFile() {
    var jsonDataSource = new JsonDataSource();
    // Specify the JSON file name.
    Uri fileUri = new Uri("customers.json", UriKind.RelativeOrAbsolute);
    jsonDataSource.JsonSource = new UriJsonSource(fileUri);
    // Populate the data source with data.
    jsonDataSource.Fill();
    return jsonDataSource;
}

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

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

    // Specify a string with JSON data.
    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);
    // Populate the data source with data.
    jsonDataSource.Fill();
    return jsonDataSource;
}

See also:

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