I am creating a project of Windows Forms Application using C#
I want to show all information(like CityName,CountryCode, etc in different Label) related to IP Address that is on my TextBox.
I already read many article about JsonConvert and I don't want to use JsonConvert.
here is my c# code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;
namespace GetIPinfo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<LocaionInfo1> locations = new List<LocaionInfo1>();
string url = string.Format("http://ipinfo.io/" + txtip.Text);
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
LocaionInfo1 location = new JavaScriptSerializer().Deserialize<LocaionInfo1>(json);
locations.Add(location);
}
if (locations.Count > 0)
{
foreach (LocaionInfo1 loc in locations)
{
label9.Text = loc.CityName;
label10.Text = loc.CountryCode;
label11.Text = loc.CountryName;
}
}
}
public class LocaionInfo1
{
public string IPAddress { get; set; }
public string CountryName { get; set; }
public string CountryCode { get; set; }
public string CityName { get; set; }
public string RegionName { get; set; }
public string ZipCode { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string TimeZone { get; set; }
}
}
}
The problem is that when i Debug this code and Enter IP in my TextBox and then click on Submit Button
My LocaionInfo1 location = new JavaScriptSerializer().Deserialize<LocaionInfo1>(json);
is NULL values.
JSON Data is:
{
"ip": "182.69.151.41",
"hostname": "abts-north-dynamic-041.151.69.182.airtelbroadband.in",
"city": null,
"country": "IN",
"loc": "20.0000,77.0000",
"org": "AS24560 Bharti Airtel Ltd., Telemedia Services"
}
So please help to get these all values. I am using Visual Studio 2012.