corelibraries-devexpress-dot-dataaccess-dot-json.md
The read-only data source that retrieves and provides JSON data.
Namespace : DevExpress.DataAccess.Json
Assembly : DevExpress.DataAccess.v25.2.dll
NuGet Package : DevExpress.DataAccess
[ToolboxBitmap(typeof(JsonDataSource), "JsonDataSource.bmp")]
public class JsonDataSource :
DataComponentBase,
ITypedList,
IListAdapter,
IList,
ICollection,
IEnumerable,
IBindingList,
ISupportFillAsync,
IListAdapterAsync,
IDynamicLookupSettingsDataProvider
<ToolboxBitmap(GetType(JsonDataSource), "JsonDataSource.bmp")>
Public Class JsonDataSource
Inherits DataComponentBase
Implements ITypedList,
IListAdapter,
IList,
ICollection,
IEnumerable,
IBindingList,
ISupportFillAsync,
IListAdapterAsync,
IDynamicLookupSettingsDataProvider
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.
JsonDataSource and DashboardJsonDataSource are read-only data sources.
You can create a JsonDataSource object at design time or at runtime.
Use one of the options below to create a JsonDataSource.
Add the JsonDataSource** component from the Toolbox (for WinForms controls)
Invoke the Data Source Configuration Wizard (for WinForms controls)
Invoke the Report Data Source Wizard (for XtraReports)
After a JsonDataSource class instance is created, you can access and customize this instance in the Properties window.
Follow the steps below to configure a JsonDataSource class instance:
Use the JsonDataSource.JsonSource property to specify the JSON data location.
(Optionally) Use the JsonDataSource.Schema property to specify from which JSON nodes to retrieve data.
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;
using DevExpress.XtraReports.UI;
public static JsonDataSource CreateDataSourceFromWeb() {
var jsonDataSource = new JsonDataSource();
// Specify the endpoint.
jsonDataSource.JsonSource = new UriJsonSource(
new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"));
var root = new JsonSchemaNode();
root.NodeType = JsonNodeType.Object;
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))),
new JsonSchemaNode(new JsonNode("Address", false, JsonNodeType.Property, typeof(string)))
});
root.AddChildren(customers);
jsonDataSource.Schema = root;
// The schema is built, you do not have to call the Fill method to populate the Field List.
// The Designer calls the Fill method automatically when a document is generated for preview.
//jsonDataSource.Fill();
return jsonDataSource;
}
Imports System.Net
Imports DevExpress.DataAccess.Json
Imports DevExpress.XtraReports.UI
Public Shared Function CreateDataSourceFromWeb() As JsonDataSource
Dim jsonDataSource = New JsonDataSource()
' Specify the endpoint.
jsonDataSource.JsonSource = New UriJsonSource(
New Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"))
Dim root = New JsonSchemaNode()
root.NodeType = JsonNodeType.Object
Dim customers = New JsonSchemaNode() With {.NodeType = JsonNodeType.Array, .Name = "Customers", .Selected = True}
customers.AddChildren({
New JsonSchemaNode(New JsonNode("CustomerID", True, JsonNodeType.Property,
GetType(String))) With {.DisplayName = "Customer ID"},
New JsonSchemaNode() With {.Name = "CompanyName", .Selected = True,
.NodeType = JsonNodeType.Property, .Type = GetType(String)},
New JsonSchemaNode(New JsonNode("ContactTitle", True, JsonNodeType.Property,
GetType(String))),
New JsonSchemaNode(New JsonNode("Address", False, JsonNodeType.Property,
GetType(String)))
})
root.AddChildren(customers)
jsonDataSource.Schema = root
' The schema is built, you do not have to call the Fill method to populate the Field List.
' The Designer calls the Fill method automatically when a document is generated for preview.
'jsonDataSource.Fill()
Return jsonDataSource
End Function
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 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;
}
Imports System.Net
Imports DevExpress.DataAccess.Json
Imports DevExpress.XtraReports.UI
Public Shared Function CreateDataSourceFromFile() As JsonDataSource
Dim jsonDataSource = New JsonDataSource()
' Specify the JSON file name.
Dim fileUri As New Uri("customers.json", UriKind.RelativeOrAbsolute)
jsonDataSource.JsonSource = New UriJsonSource(fileUri)
' Populate the data source with data.
jsonDataSource.Fill()
Return jsonDataSource
End Function
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 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;
}
Imports System.Net
Imports DevExpress.DataAccess.Json
Imports DevExpress.XtraReports.UI
Public Shared Function CreateDataSourceFromText() As JsonDataSource
Dim jsonDataSource = New JsonDataSource()
' Specify a string with JSON data.
Dim json As String = "{""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
End Function
See also:
The JsonDataSource object uses the System.Text.Json library. Install the Newtonsoft.Json package if your application does not reference this library. .NET projects do not require manual installation of the System.Text.Json package, as it is already included in the .NET environment. Set the DevExpress.DataAccess.Native.Json.JsonLoaderHelper.ProcessingLibrary property to NewtonsoftJson to use the Newtonsoft.Json library instead.
Object MarshalByRefObject Component DataComponentBase JsonDataSource DashboardJsonDataSource
See Also
How to Create the Data Access Library Data Sources at Runtime
Provide Authentication to Access JSON Data (Runtime Sample)
Bind a Report to JSON Data (Runtime Sample)