Hi rani,
Check this example. Now please take its reference and correct your code.
For reading json from url i have used below article.
Read (Parse) JSON data from URL and display in ASP.Net GridView using C# and VB.Net
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', []);
app.controller('MyController', function ($scope, $http, $window) {
$http.post("Default.aspx/PopulateCountries", { headers: { 'Content-Type': 'application/json'} })
.then(function (response) {
$scope.Customers = response.data.d;
}, function error(response) {
alert(response.responseText);
});
});
</script>
<div ng-app="MyApp" ng-controller="MyController">
<table>
<thead>
<tr>
<th>Name</th>
<th>Capital</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="customer in Customers">
<td>{{ customer.Name }}</td>
<td>{{ customer.Capital }}</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>
Namespaces
C#
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Web.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
VB.Net
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Linq
Imports System.Net
Imports System.Web.Services
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Code
C#
[WebMethod]
public static List<Country> PopulateCountries()
{
List<Country> countries = new List<Country>();
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in cultures)
{
RegionInfo region = new RegionInfo(culture.LCID);
string countryName = region.EnglishName;
string countryCode = "";
if (region.EnglishName.ToUpper().Contains(countryName.ToUpper()))
{
countryCode = region.TwoLetterISORegionName;
}
string capitalName = "";
if (!countryCode.All(Char.IsDigit))
{
capitalName = GetCapital(countryCode);
}
countries.Add(new Country { Name = countryName, Capital = capitalName });
}
return countries.Distinct(new ItemEqualityComparer()).ToList();
}
public class ItemEqualityComparer : IEqualityComparer<Country>
{
public bool Equals(Country x, Country y)
{
// Two items are equal if their keys are equal.
return x.Name == y.Name;
}
public int GetHashCode(Country obj)
{
return obj.Name.GetHashCode();
}
}
// Get Capital name from TwoLetterISORegionName.
public static string GetCapital(string countryCode)
{
string capitalName = "";
string url = string.Format("http://www.geognos.com/api/en/countries/info/{0}.json", countryCode);
using (WebClient client = new WebClient())
{
try
{
string json = client.DownloadString(url);
JObject weatherInfo = JsonConvert.DeserializeObject<JObject>(json);
if (((JToken)weatherInfo["Results"])["Capital"].ToString() != "null")
{
capitalName = ((JToken)weatherInfo["Results"])["Capital"]["Name"].ToString().Replace("\"", string.Empty);
}
else
{
capitalName = "";
}
}
catch (Exception)
{
}
}
return capitalName;
}
public class Country
{
public string Name { get; set; }
public string Capital { get; set; }
}
VB.Net
<WebMethod()>
Public Shared Function PopulateCountries() As List(Of Country)
Dim countries As List(Of Country) = New List(Of Country)()
Dim cultures As CultureInfo() = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
For Each culture As CultureInfo In cultures
Dim region As RegionInfo = New RegionInfo(culture.LCID)
Dim countryName As String = region.EnglishName
Dim countryCode As String = ""
If region.EnglishName.ToUpper().Contains(countryName.ToUpper()) Then
countryCode = region.TwoLetterISORegionName
End If
Dim capitalName As String = ""
If Not countryCode.All(AddressOf Char.IsDigit) Then
capitalName = GetCapital(countryCode)
End If
countries.Add(New Country With {
.Name = countryName,
.Capital = capitalName
})
Next
Return countries.Distinct(New ItemEqualityComparer()).ToList()
End Function
Public Class ItemEqualityComparer
Implements IEqualityComparer(Of Country)
Public Overloads Function Equals(ByVal x As Country, ByVal y As Country) As Boolean Implements IEqualityComparer(Of Country).Equals
Return x.Name = y.Name
End Function
Public Overloads Function GetHashCode(ByVal obj As Country) As Integer Implements IEqualityComparer(Of Country).GetHashCode
Return obj.Name.GetHashCode()
End Function
End Class
Public Shared Function GetCapital(ByVal countryCode As String) As String
Dim capitalName As String = ""
Dim url As String = String.Format("http://www.geognos.com/api/en/countries/info/{0}.json", countryCode)
Using client As WebClient = New WebClient()
Try
Dim json As String = client.DownloadString(url)
Dim weatherInfo As JObject = JsonConvert.DeserializeObject(Of JObject)(json)
If (CType(weatherInfo("Results"), JToken))("Capital").ToString() <> "null" Then
capitalName = (CType(weatherInfo("Results"), JToken))("Capital")("Name").ToString().Replace("""", String.Empty)
Else
capitalName = ""
End If
Catch ex As Exception
End Try
End Using
Return capitalName
End Function
Public Class Country
Public Property Name As String
Public Property Capital As String
End Class
Screenshot