Back to Devexpress

JsonDataSource.JsonSource Property

corelibraries-devexpress-dot-dataaccess-dot-json-dot-jsondatasource-e83f2109.md

latest13.5 KB
Original Source

JsonDataSource.JsonSource Property

Gets or sets an object that stores JSON data import settings.

Namespace : DevExpress.DataAccess.Json

Assembly : DevExpress.DataAccess.v25.2.dll

NuGet Package : DevExpress.DataAccess

Declaration

csharp
[DefaultValue(null)]
[LocalizableCategory(DataAccessStringId.PropertyGridDataCategoryName)]
public JsonSourceBase JsonSource { get; set; }
vb
<DefaultValue(Nothing)>
<LocalizableCategory(DataAccessStringId.PropertyGridDataCategoryName)>
Public Property JsonSource As JsonSourceBase

Property Value

TypeDefaultDescription
JsonSourceBasenull

A JsonSourceBase descendant that specifies the JSON data location.

|

Remarks

Use the JsonSource property to specify the JSON data location. Set the JsonSource property to a JsonSourceBase descendant.

JSON Data LocationJsonSource Property Value
Web-service endpointA UriJsonSource object
Text fileA UriJsonSource object
JSON stringA CustomJsonSource object

Examples

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

csharp
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;
        }
vb
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.

csharp
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;
}
vb
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.

csharp
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;
}
vb
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

The following code snippets (auto-collected from DevExpress Examples) contain references to the JsonSource property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-dashboard-json-data-source/CS/DashboardJsonExample/Form1.cs#L50

csharp
var jsonDataSource = new DashboardJsonDataSource();
jsonDataSource.JsonSource = new UriJsonSource(new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"));
jsonDataSource.RootElement = "Customers";

reporting-winforms-create-report-bound-to-json-data-source/CS/Form1.cs#L62

csharp
// Specify the endpoint.
jsonDataSource.JsonSource = new UriJsonSource(
    new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"));

asp-net-mvc-dashboard-register-data-sources/CS/MvcDashboardDataSources/Configuration/JsonDataSourceConfigurator.cs#L12

csharp
DashboardJsonDataSource jsonDataSourceUrl = new DashboardJsonDataSource("JSON Data Source (URL)");
jsonDataSourceUrl.JsonSource = new UriJsonSource(new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"));
jsonDataSourceUrl.RootElement = "Customers";

data-access-library-create-data-sources-at-runtime/CS/JsonDataSourceSnippets/Code.cs#L15

csharp
Uri sourceUri = new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json");
jsonDataSource.JsonSource = new DevExpress.DataAccess.Json.UriJsonSource(sourceUri);
jsonDataSource.Fill();

reporting-web-forms-report-designer-add-data-sources/CS/WebApplication1/Default.aspx.cs#L30

csharp
var uri = new System.Uri("~/App_Data/nwind.json", System.UriKind.Relative);
jsonDataSource.JsonSource = new DevExpress.DataAccess.Json.UriJsonSource(uri);
jsonDataSource.Fill();

winforms-dashboard-json-data-source/VB/DashboardJsonExample/Form1.vb#L47

vb
Dim jsonDataSource = New DashboardJsonDataSource()
jsonDataSource.JsonSource = New UriJsonSource(New Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"))
jsonDataSource.RootElement = "Customers"

reporting-winforms-create-report-bound-to-json-data-source/VB/Form1.vb#L53

vb
' Specify the endpoint.
jsonDataSource.JsonSource = New UriJsonSource(
    New Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"))

reporting-winforms-provide-authentication-to-access-json-data/VB/Form1.vb#L104

vb
Dim jsonDS = (TryCast(component, DevExpress.DataAccess.Json.JsonDataSource))
Dim jsonSource = (TryCast(jsonDS?.JsonSource, MyUriJsonSource))
If jsonSource IsNot Nothing Then

asp-net-mvc-dashboard-register-data-sources/VB/MvcDashboardDataSources/Configuration/JsonDataSourceConfigurator.vb#L12

vb
Dim jsonDataSourceUrl As New DashboardJsonDataSource("JSON Data Source (URL)")
jsonDataSourceUrl.JsonSource = New UriJsonSource(New Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"))
jsonDataSourceUrl.RootElement = "Customers"

data-access-library-create-data-sources-at-runtime/VB/JsonDataSourceSnippets/Code.vb#L12

vb
Dim sourceUri As New Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json")
jsonDataSource.JsonSource = New DevExpress.DataAccess.Json.UriJsonSource(sourceUri)
jsonDataSource.Fill()

See Also

Bind a Report to JSON Data (Runtime Sample)

JsonDataSource Class

JsonDataSource Members

DevExpress.DataAccess.Json Namespace