Hi alex0230,
Refer below modified code.
Namespaces
C#
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
using Newtonsoft.Json;
VB.Net
Imports System.Data
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Net
Imports Newtonsoft.Json
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
List<Root> roots = JsonConvert.DeserializeObject<List<Root>>(File.ReadAllText(Server.MapPath("~/json.json")));
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Latitude");
dt.Columns.Add("Longitude");
dt.Columns.Add("Description");
foreach (Root root in roots)
{
string CompName = "", addressLine = "", CompCity = "", CompState = "", CompZip = "";
string description = "0";
if (!string.IsNullOrEmpty(root.company.name))
{
CompName = root.company.name;
}
if (!string.IsNullOrEmpty(root.addressLine1))
{
addressLine = root.addressLine1;
}
if (!string.IsNullOrEmpty(root.city))
{
CompCity = root.city;
}
if (!string.IsNullOrEmpty(root.stateIdentifier))
{
CompState = root.stateIdentifier;
}
if (!string.IsNullOrEmpty(root.zip))
{
CompZip = root.zip;
}
string address = string.Format("{0}+{1}+{2}+{3}", addressLine.ToString(), CompCity.ToString(), CompState.ToString(), CompZip.ToString());
if (!string.IsNullOrEmpty(address.Replace("+", "")))
{
double latitude = GetLatLon(address).Latitude;
double longitude = GetLatLon(address).Longitude;
dt.Rows.Add(CompName, latitude, longitude, description);
}
}
var result = (from customer in dt.AsEnumerable()
group customer by customer["Name"] into g
orderby g.Count() descending
select g).Take(10);
DataTable dtFinal = dt.Clone();
foreach (var item in result)
{
DataRow dr = dt.Select("Name='" + item.Key + "'")[0];
dtFinal.Rows.Add(dr.ItemArray);
}
}
public LatLon GetLatLon(string address)
{
string url = string.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}&key=API_Key", Server.UrlEncode(address));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
try
{
if (response.StatusCode == HttpStatusCode.OK)
{
MemoryStream ms = new MemoryStream();
Stream responseStream = response.GetResponseStream();
byte[] buffer = new byte[2049];
int count = responseStream.Read(buffer, 0, buffer.Length);
while (count > 0)
{
ms.Write(buffer, 0, count);
count = responseStream.Read(buffer, 0, buffer.Length);
}
responseStream.Close();
ms.Close();
byte[] responseBytes = ms.ToArray();
ASCIIEncoding encoding = new ASCIIEncoding();
string coords = encoding.GetString(responseBytes);
Root1 result = JsonConvert.DeserializeObject<Root1>(coords);
if (result.results.Count > 0)
{
return new LatLon()
{
Latitude = result.results[0].geometry.location.lat,
Longitude = result.results[0].geometry.location.lng
};
}
else
{
return new LatLon() { };
}
}
}
catch (Exception ex)
{
}
return new LatLon() { };
}
public class LatLon
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { get; set; }
}
public class Geometry
{
public Location1 location { get; set; }
public string location_type { get; set; }
public Viewport viewport { get; set; }
}
public class Location1
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class PlusCode
{
public string compound_code { get; set; }
public string global_code { get; set; }
}
public class Result
{
public List<AddressComponent> address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public string place_id { get; set; }
public PlusCode plus_code { get; set; }
public List<string> types { get; set; }
}
public class Root1
{
public List<Result> results { get; set; }
public string status { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Viewport
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Agreement
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Board
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Company
{
public int id { get; set; }
public string identifier { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Contact
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Country
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Currency
{
public int id { get; set; }
public string symbol { get; set; }
public string currencyCode { get; set; }
public string decimalSeparator { get; set; }
public int numberOfDecimals { get; set; }
public string thousandsSeparator { get; set; }
public bool negativeParenthesesFlag { get; set; }
public bool displaySymbolFlag { get; set; }
public string currencyIdentifier { get; set; }
public bool displayIdFlag { get; set; }
public bool rightAlign { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class CustomField
{
public int id { get; set; }
public string caption { get; set; }
public string type { get; set; }
public string entryMethod { get; set; }
public int numberOfDecimals { get; set; }
public string value { get; set; }
}
public class Department
{
public int id { get; set; }
public string identifier { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Info
{
public string board_href { get; set; }
public string priority_href { get; set; }
public string image_href { get; set; }
public string location_href { get; set; }
public string source_href { get; set; }
public string department_href { get; set; }
public string sla_href { get; set; }
public string currency_href { get; set; }
public DateTime lastUpdated { get; set; }
public string updatedBy { get; set; }
public DateTime dateEntered { get; set; }
public string enteredBy { get; set; }
public string activities_href { get; set; }
public string scheduleentries_href { get; set; }
public string documents_href { get; set; }
public string configurations_href { get; set; }
public string tasks_href { get; set; }
public string notes_href { get; set; }
public string products_href { get; set; }
public string timeentries_href { get; set; }
public string expenseEntries_href { get; set; }
public string agreement_href { get; set; }
public string type_href { get; set; }
public string status_href { get; set; }
public string member_href { get; set; }
public string workRole_href { get; set; }
public string workType_href { get; set; }
public string company_href { get; set; }
public string mobileGuid { get; set; }
public string site_href { get; set; }
public string country_href { get; set; }
public string contact_href { get; set; }
public string team_href { get; set; }
}
public class Location
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Owner
{
public int id { get; set; }
public string identifier { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Priority
{
public int id { get; set; }
public string name { get; set; }
public int sort { get; set; }
public Info _info { get; set; }
}
public class Root
{
public int id { get; set; }
public string summary { get; set; }
public string recordType { get; set; }
public Board board { get; set; }
public Status status { get; set; }
public WorkRole workRole { get; set; }
public WorkType workType { get; set; }
public Company company { get; set; }
public Site site { get; set; }
public string siteName { get; set; }
public string addressLine1 { get; set; }
public string city { get; set; }
public string stateIdentifier { get; set; }
public Country country { get; set; }
public Contact contact { get; set; }
public string contactName { get; set; }
public string contactPhoneNumber { get; set; }
public string contactEmailAddress { get; set; }
public Team team { get; set; }
public Priority priority { get; set; }
public ServiceLocation serviceLocation { get; set; }
public Source source { get; set; }
public string severity { get; set; }
public string impact { get; set; }
public bool allowAllClientsPortalView { get; set; }
public bool customerUpdatedFlag { get; set; }
public bool automaticEmailContactFlag { get; set; }
public bool automaticEmailResourceFlag { get; set; }
public bool automaticEmailCcFlag { get; set; }
public bool closedFlag { get; set; }
public bool approved { get; set; }
public double estimatedExpenseCost { get; set; }
public double estimatedExpenseRevenue { get; set; }
public double estimatedProductCost { get; set; }
public double estimatedProductRevenue { get; set; }
public double estimatedTimeCost { get; set; }
public double estimatedTimeRevenue { get; set; }
public string billingMethod { get; set; }
public string subBillingMethod { get; set; }
public int resolveMinutes { get; set; }
public int resPlanMinutes { get; set; }
public int respondMinutes { get; set; }
public bool isInSla { get; set; }
public bool hasChildTicket { get; set; }
public bool hasMergedChildTicketFlag { get; set; }
public string billTime { get; set; }
public string billExpenses { get; set; }
public string billProducts { get; set; }
public Location location { get; set; }
public Department department { get; set; }
public string mobileGuid { get; set; }
public Sla sla { get; set; }
public Currency currency { get; set; }
public Info _info { get; set; }
public DateTime escalationStartDateUTC { get; set; }
public int escalationLevel { get; set; }
public int minutesBeforeWaiting { get; set; }
public int respondedSkippedMinutes { get; set; }
public int resplanSkippedMinutes { get; set; }
public List<CustomField> customFields { get; set; }
public string zip { get; set; }
public Agreement agreement { get; set; }
public string slaStatus { get; set; }
public Type type { get; set; }
public double? actualHours { get; set; }
public string resources { get; set; }
public bool? requestForChangeFlag { get; set; }
public string automaticEmailCc { get; set; }
public DateTime? dateResplan { get; set; }
public DateTime? dateResponded { get; set; }
public string addressLine2 { get; set; }
public Owner owner { get; set; }
}
public class ServiceLocation
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Site
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Sla
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Source
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Status
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Team
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class Type
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class WorkRole
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
public class WorkType
{
public int id { get; set; }
public string name { get; set; }
public Info _info { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim roots As List(Of Root) = JsonConvert.DeserializeObject(Of List(Of Root))(File.ReadAllText(Server.MapPath("~/json.json")))
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Name")
dt.Columns.Add("Latitude")
dt.Columns.Add("Longitude")
dt.Columns.Add("Description")
For Each root As Root In roots
Dim CompName As String = "", addressLine As String = "", CompCity As String = "", CompState As String = "", CompZip As String = ""
Dim description As String = "0"
If Not String.IsNullOrEmpty(root.company.name) Then
CompName = root.company.name
End If
If Not String.IsNullOrEmpty(root.addressLine1) Then
addressLine = root.addressLine1
End If
If Not String.IsNullOrEmpty(root.city) Then
CompCity = root.city
End If
If Not String.IsNullOrEmpty(root.stateIdentifier) Then
CompState = root.stateIdentifier
End If
If Not String.IsNullOrEmpty(root.zip) Then
CompZip = root.zip
End If
Dim address As String = String.Format("{0}+{1}+{2}+{3}", addressLine.ToString(), CompCity.ToString(), CompState.ToString(), CompZip.ToString())
If Not String.IsNullOrEmpty(address.Replace("+", "")) Then
Dim latitude As Double = GetLatLon(address).Latitude
Dim longitude As Double = GetLatLon(address).Longitude
dt.Rows.Add(CompName, latitude, longitude, description)
End If
Next
Dim result = (From customer In dt.AsEnumerable()
Group customer By Key = customer("Name") Into grp = Group
Order By grp.Count() Descending
Select grp).Take(10)
Dim dtFinal As DataTable = dt.Clone()
For Each item In result
Dim dr As DataRow = dt.Select("Name='" & DirectCast(item, DataRow())(0)("Name") & "'")(0)
dtFinal.Rows.Add(dr.ItemArray)
Next
End Sub
Public Function GetLatLon(ByVal address As String) As LatLon
Dim url As String = String.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}&key=API_Key", Server.UrlEncode(address))
Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Try
If response.StatusCode = HttpStatusCode.OK Then
Dim ms As MemoryStream = New MemoryStream()
Dim responseStream As Stream = response.GetResponseStream()
Dim buffer As Byte() = New Byte(2048) {}
Dim count As Integer = responseStream.Read(buffer, 0, buffer.Length)
While count > 0
ms.Write(buffer, 0, count)
count = responseStream.Read(buffer, 0, buffer.Length)
End While
responseStream.Close()
ms.Close()
Dim responseBytes As Byte() = ms.ToArray()
Dim encoding As ASCIIEncoding = New ASCIIEncoding()
Dim coords As String = encoding.GetString(responseBytes)
Dim result As Root1 = JsonConvert.DeserializeObject(Of Root1)(coords)
If result.results.Count > 0 Then
Return New LatLon() With {
.Latitude = result.results(0).geometry.location.lat,
.Longitude = result.results(0).geometry.location.lng
}
Else
Return New LatLon()
End If
End If
Catch ex As Exception
End Try
Return New LatLon()
End Function
Public Class LatLon
Public Property Latitude As Double
Public Property Longitude As Double
End Class
Public Class AddressComponent
Public Property long_name As String
Public Property short_name As String
Public Property types As List(Of String)
End Class
Public Class Geometry
Public Property location As Location1
Public Property location_type As String
Public Property viewport As Viewport
End Class
Public Class Location1
Public Property lat As Double
Public Property lng As Double
End Class
Public Class Northeast
Public Property lat As Double
Public Property lng As Double
End Class
Public Class PlusCode
Public Property compound_code As String
Public Property global_code As String
End Class
Public Class Result
Public Property address_components As List(Of AddressComponent)
Public Property formatted_address As String
Public Property geometry As Geometry
Public Property place_id As String
Public Property plus_code As PlusCode
Public Property types As List(Of String)
End Class
Public Class Root1
Public Property results As List(Of Result)
Public Property status As String
End Class
Public Class Southwest
Public Property lat As Double
Public Property lng As Double
End Class
Public Class Viewport
Public Property northeast As Northeast
Public Property southwest As Southwest
End Class
Public Class Agreement
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Board
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Company
Public Property id As Integer
Public Property identifier As String
Public Property name As String
Public Property _info As Info
End Class
Public Class Contact
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Country
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Currency
Public Property id As Integer
Public Property symbol As String
Public Property currencyCode As String
Public Property decimalSeparator As String
Public Property numberOfDecimals As Integer
Public Property thousandsSeparator As String
Public Property negativeParenthesesFlag As Boolean
Public Property displaySymbolFlag As Boolean
Public Property currencyIdentifier As String
Public Property displayIdFlag As Boolean
Public Property rightAlign As Boolean
Public Property name As String
Public Property _info As Info
End Class
Public Class CustomField
Public Property id As Integer
Public Property caption As String
Public Property type As String
Public Property entryMethod As String
Public Property numberOfDecimals As Integer
Public Property value As String
End Class
Public Class Department
Public Property id As Integer
Public Property identifier As String
Public Property name As String
Public Property _info As Info
End Class
Public Class Info
Public Property board_href As String
Public Property priority_href As String
Public Property image_href As String
Public Property location_href As String
Public Property source_href As String
Public Property department_href As String
Public Property sla_href As String
Public Property currency_href As String
Public Property lastUpdated As DateTime
Public Property updatedBy As String
Public Property dateEntered As DateTime
Public Property enteredBy As String
Public Property activities_href As String
Public Property scheduleentries_href As String
Public Property documents_href As String
Public Property configurations_href As String
Public Property tasks_href As String
Public Property notes_href As String
Public Property products_href As String
Public Property timeentries_href As String
Public Property expenseEntries_href As String
Public Property agreement_href As String
Public Property type_href As String
Public Property status_href As String
Public Property member_href As String
Public Property workRole_href As String
Public Property workType_href As String
Public Property company_href As String
Public Property mobileGuid As String
Public Property site_href As String
Public Property country_href As String
Public Property contact_href As String
Public Property team_href As String
End Class
Public Class Location
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Owner
Public Property id As Integer
Public Property identifier As String
Public Property name As String
Public Property _info As Info
End Class
Public Class Priority
Public Property id As Integer
Public Property name As String
Public Property sort As Integer
Public Property _info As Info
End Class
Public Class Root
Public Property id As Integer
Public Property summary As String
Public Property recordType As String
Public Property board As Board
Public Property status As Status
Public Property workRole As WorkRole
Public Property workType As WorkType
Public Property company As Company
Public Property site As Site
Public Property siteName As String
Public Property addressLine1 As String
Public Property city As String
Public Property stateIdentifier As String
Public Property country As Country
Public Property contact As Contact
Public Property contactName As String
Public Property contactPhoneNumber As String
Public Property contactEmailAddress As String
Public Property team As Team
Public Property priority As Priority
Public Property serviceLocation As ServiceLocation
Public Property source As Source
Public Property severity As String
Public Property impact As String
Public Property allowAllClientsPortalView As Boolean
Public Property customerUpdatedFlag As Boolean
Public Property automaticEmailContactFlag As Boolean
Public Property automaticEmailResourceFlag As Boolean
Public Property automaticEmailCcFlag As Boolean
Public Property closedFlag As Boolean
Public Property approved As Boolean
Public Property estimatedExpenseCost As Double
Public Property estimatedExpenseRevenue As Double
Public Property estimatedProductCost As Double
Public Property estimatedProductRevenue As Double
Public Property estimatedTimeCost As Double
Public Property estimatedTimeRevenue As Double
Public Property billingMethod As String
Public Property subBillingMethod As String
Public Property resolveMinutes As Integer
Public Property resPlanMinutes As Integer
Public Property respondMinutes As Integer
Public Property isInSla As Boolean
Public Property hasChildTicket As Boolean
Public Property hasMergedChildTicketFlag As Boolean
Public Property billTime As String
Public Property billExpenses As String
Public Property billProducts As String
Public Property location As Location
Public Property department As Department
Public Property mobileGuid As String
Public Property sla As Sla
Public Property currency As Currency
Public Property _info As Info
Public Property escalationStartDateUTC As DateTime
Public Property escalationLevel As Integer
Public Property minutesBeforeWaiting As Integer
Public Property respondedSkippedMinutes As Integer
Public Property resplanSkippedMinutes As Integer
Public Property customFields As List(Of CustomField)
Public Property zip As String
Public Property agreement As Agreement
Public Property slaStatus As String
Public Property type As Type
Public Property actualHours As Double?
Public Property resources As String
Public Property requestForChangeFlag As Boolean?
Public Property automaticEmailCc As String
Public Property dateResplan As DateTime?
Public Property dateResponded As DateTime?
Public Property addressLine2 As String
Public Property owner As Owner
End Class
Public Class ServiceLocation
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Site
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Sla
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Source
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Status
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Team
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class Type
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class WorkRole
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Public Class WorkType
Public Property id As Integer
Public Property name As String
Public Property _info As Info
End Class
Screenshot