In this article I will explain with an example, how to read (parse) JSON data from URL and display in ASP.Net DataList using C# and VB.Net.
Download JSON.Net library
The JSON.Net library is available for download from the following URL.
JSON File URL
This article uses the following REST API hosted on GitHub.
The following JSON string is returned from the API.
[
{
"CustomerId": 1,
"Name": "John Hammond",
"Country": "United States"
},
{
"CustomerId": 2,
"Name": "Mudassar Khan",
"Country": "India"
},
{
"CustomerId": 3,
"Name": "Suzanne Mathews",
"Country": "France"
},
{
"CustomerId": 4,
"Name": "Robert Schidner",
"Country": "Russia"
}
]
HTML Markup
The following HTML Markup consists of:
DataList – For displaying data.
Templates
HeaderTemplate – The content of this template will not be repeated and will be placed in the top most position i.e. head section of the DataList control.
ItemTemplate – The content of this template will be repeated for each record present in its DataSource.
FooterTemplate – The content of this template will not be repeated and will be placed in the bottom most position i.e. footer section of the DataList control.
Controls
ItemTemplate consists of three Label control for displaying CustomerId, Name and Country of Customers.
<asp:DataList ID="dlCustomers" runat="server">
<HeaderTemplate>
<table class="Grid">
<tr>
<th>Customer Id</th>
<th>Name</th>
<th>Country</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" Text='<%# Eval("CustomerId") %>'></asp:Label></td>
<td><asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label></td>
<td><asp:Label runat="server" Text='<%# Eval("Country") %>'></asp:Label></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
Namespaces
You will need to import the following namespaces.
C#
using System.Net;
using System.Data;
using Newtonsoft.Json;
VB.Net
Imports System.Net
Imports System.Data
Imports Newtonsoft.Json
Reading (Parsing) JSON data from URL and display in ASP.Net DataList
Inside the Page Load event handler, first the Security Protocol is set.
The JSON string is downloaded from an API using DownloadString method of the WebClient class.
DownloadString:
It accepts the following parameter:
URL – The URL of the file to be downloaded.
The downloaded JSON string is then converted to DataTable using DeserializeObject method of JsonConvert class.
Finally, the DataTable is assigned to the DataSource property of the DataList and the DataList is populated.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
string json = (new WebClient()).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json");
dlCustomers.DataSource = JsonConvert.DeserializeObject<DataTable>(json);
dlCustomers.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Dim json As String = (New WebClient()).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json")
dlCustomers.DataSource = JsonConvert.DeserializeObject(Of DataTable)(json)
dlCustomers.DataBind()
End If
End Sub
Screenshot
Demo
Downloads