winform中的Json解析

http://whois.hichina.com/whois/api_whois?host=www.hao123.cn&oper=0这个网址会得到一个json数据

如下所示

{"exp_date":" 2015-03-17 12:48:36",
"name_server":"f1g1ns1.dnspod.net,f1g1ns2.dnspod.net",
"status":" ok",
"name":"hao123.cn",
"email":" suyouzhong@gmail.com",
"registrar":"厦门市中资源网络服务有限公司",
"reg_date":" 2003-03-17 12:20:05",
"total_infor":"Domain Name: hao123.cn\nROID: 20030311s10001s00039363-cn\nDomain Status: ok\nRegistrant ID: cnnic-zdmd-015\nRegistrant: 厦门中越网络科技有限公司\nRegistrant Contact Email: suyouzhong@gmail.com\nSponsoring Registrar: 厦门市中资源网络服务有限公司\nName Server: f1g1ns1.dnspod.net\nName Server: f1g1ns2.dnspod.net\nRegistration Date: 2003-03-17 12:20:05\nExpiration Date: 2015-03-17 12:48:36\nDNSSEC: unsigned\n"
,"Registrant_Organization":" 厦门中越网络科技有限公司"
}

我要把这个json数据显示到一个DataGridView,只显示域名、注册商、注册时间、到期时间,如下图所示

 

源码如下

public partial class frm_WhoIs : Form
{
public frm_WhoIs()
{
InitializeComponent();
}
private static frm_WhoIs a = null;

public static frm_WhoIs getForm()
{
if (a == null || a.IsDisposed == true)
{
a = new frm_WhoIs();
}
return a;

}
/// <summary>
/// 点击查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, EventArgs e)
{
string par = IsVal();
if (par == null)
{
MessageBox.Show("请输入要查询的域名");
return;
}
if (par.Length == 0 || par == "")
{
MessageBox.Show("请输入要查询的域名");
return;
}
string url = "http://whois.hichina.com/whois/api_whois?host=" + par + "&oper=0";//所要获得内容的页面
var Url = url;
WebClient Client = new WebClient();
Client.Credentials = CredentialCache.DefaultCredentials;
byte[] PageData = Client.DownloadData(@Url);
string PageHtml = Encoding.Default.GetString(PageData);
DataTable dt = new DataTable();
if (!dt.Columns.Contains("name"))
{
dt.Columns.Add("name", typeof(string));
}
if (!dt.Columns.Contains("registrar"))
{
dt.Columns.Add("registrar", typeof(string));
}
if (!dt.Columns.Contains("creation"))
{
dt.Columns.Add("creation", typeof(DateTime));
}
if (!dt.Columns.Contains("expiration"))
{
dt.Columns.Add("expiration", typeof(DateTime));
}
DataRow dr = dt.NewRow();
WhoIsData whois = SerizaleTrackdalkDataTi(PageHtml);
try
{
string name = whois.name.ToString();
string reg = whois.registrar.ToString();
string create = whois.reg_date.ToString();
string exp = whois.exp_date.ToString();
dr[0] = name;
dr[1] = reg;
dr[2] = create;
dr[3] = exp;
}
catch (Exception)
{
dr[0] = txt_Whois.Text;
dr[1] = "无信息";
}
dt.Rows.Add(dr);
dgvlist.DataSource = dt;
dgvlist.AutoGenerateColumns = false;
}

/// <summary>
/// 验证输入的url
/// </summary>
/// <returns></returns>
private string IsVal()
{
string str = txt_Whois.Text.Trim();
if (str.IndexOf("www") == 0 && str.Length > 2)
{
str = str.Remove(str.IndexOf("www"), 3).Trim();
}
if (str.IndexOf("https") == 0 && str.Length > 4)
{
str = str.Remove(str.IndexOf("https"), 5).Trim();
}
if (str.IndexOf("http") == 0 && str.Length > 3)
{
str = str.Remove(str.IndexOf("http"), 4).Trim();
}
if (str.IndexOf("//") >= 0 && str.Length > 1)
{
str = str.Remove(str.IndexOf("//"), 2).Trim();
}
if (str.IndexOf(".") == 0 && str.Length > 0)
{
str = str.Remove((str.IndexOf(".")), 1).Trim();
}
if ((str.IndexOf("/") == 0 || str.IndexOf("/") == str.Length) && str.Length > 0)
{
str = str.Remove(str.IndexOf("/"), 1).Trim();
}
if(str.IndexOf("/")>0)
{
str = str.Remove(str.IndexOf("/"), str.Length - str.IndexOf("/"));
}
if (str.IndexOf(".") < 0)
{
return null;
}
if (str.IndexOf(":") >= 0)
{
str = str.Remove(str.IndexOf(":"), 1);
}
return str;
}


#region json对象的相关操作,这个类里面的属性必须和json中的一样,有大小写之分。
[DataContract]
public class WhoIsData
{
[DataMember(Order = 1)]
public string name { get; set; }//域名
[DataMember(Order = 2)]
public string registrar { get; set; }//注册商
[DataMember(Order = 3)]
public string reg_date { get; set; }//注册时间
[DataMember(Order = 4)]
public string exp_date { get; set; }//过期时间

public WhoIsData(string name, string regist, string reg, string exp)
{
this.name = name;
this.registrar = regist;
this.reg_date = reg;
this.exp_date = exp;
}
public WhoIsData() { }
}

public static WhoIsData SerizaleTrackdalkDataTi(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(WhoIsData));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
WhoIsData jsonObject = (WhoIsData)ser.ReadObject(ms);//将js序列化为对象
//ser.WriteObject(ms, jsonObject);这个是将对象序列化为json
ms.Close();
return jsonObject;
}
#endregion
}

posted @ 2013-05-23 15:41  清晓zzz  阅读(1221)  评论(0编辑  收藏  举报