Hi Vikash21,
Check the below example.
C#
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace DataTable_Json
{
class Program
{
static void Main(string[] args)
{
Task T = new Task(ApiCall);
T.Start();
Console.ReadLine();
}
static async void ApiCall()
{
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("http://api.ipinfodb.com/v3/ip-city/?key=3b8fbb8b954ae5daad70021283f3fd56b&ip=933.207.145.193&format=json");
response.EnsureSuccessStatusCode();
using (HttpContent content = response.Content)
{
string responseBody = await response.Content.ReadAsStringAsync();
List<Details> details = JsonConvert.DeserializeObject<List<Details>>("[" + responseBody + "]");
Console.WriteLine("StatusCode | " + "StatusMessage | " + "IpAddress | " + "CountryCode | " + "CountryName | "
+ "RegionName | " + "CityName | " + "ZipCode | " + "Latitude | " + "Longitude | " + "TimeZone | ");
foreach (Details detail in details)
{
Console.WriteLine(
(!(string.IsNullOrEmpty(detail.statusCode)) ? detail.statusCode : "Not Required") + " | " +
(!(string.IsNullOrEmpty(detail.statusMessage)) ? detail.statusMessage : "Not Required") + " | " +
(!(string.IsNullOrEmpty(detail.ipAddress)) ? detail.ipAddress : "Not Required") + " | " +
(!(string.IsNullOrEmpty(detail.countryCode)) ? detail.countryCode : "Not Required") + " | " +
(!(string.IsNullOrEmpty(detail.countryName)) ? detail.countryName : "Not Required") + " | " +
(!(string.IsNullOrEmpty(detail.regionName)) ? detail.regionName : "Not Required") + " | " +
(!(string.IsNullOrEmpty(detail.cityName)) ? detail.cityName : "Not Required") + " | " +
(!(string.IsNullOrEmpty(detail.zipCode)) ? detail.zipCode : "Not Required") + " | " +
(!(string.IsNullOrEmpty(detail.latitude)) ? detail.latitude : "Not Required") + " | " +
(!(string.IsNullOrEmpty(detail.longitude)) ? detail.longitude : "Not Required") + " | " +
(!(string.IsNullOrEmpty(detail.timeZone)) ? detail.timeZone : "Not Required") + " | ");
}
}
}
}
public class Details
{
public string statusCode { get; set; }
public string statusMessage { get; set; }
public string ipAddress { get; set; }
public string countryCode { get; set; }
public string countryName { get; set; }
public string regionName { get; set; }
public string cityName { get; set; }
public string zipCode { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string timeZone { get; set; }
}
}
}
Output
StatusCode | StatusMessage | IpAddress | CountryCode | CountryName | RegionName | CityName | ZipCode | Latitude | Longitude | TimeZone |
ERROR | Invalid API key. | 933.207.145.193 | Not Required | Not Required | Not Required | Not Required | Not Required | 0 | 0 | Not Required |