C# 动态获取JSON实体对象

 1 //获取ip物理地址的接口地址
 2         public static readonly string ipUrl = "http://ip.taobao.com/service/getIpInfo.php?ip=";
 3 
 4         public void ProcessRequest(HttpContext context)
 5         {
 6             context.Response.ContentType = "text";
 7             string ip = DTRequest.GetQueryString("Ip").Trim();
 8             BLL.Core.DT_Ip bll = new BLL.Core.DT_Ip();
 9             //调用淘宝ip查询接口,返回的json字符串
10             string ipJson = GetInfo(ipUrl + DTRequest.GetQueryString("Ip"));
11             var jsonModel = JsonConvert.DeserializeObject<dynamic>(ipJson);
12             var model = new Model.Core.DT_Ip();
13             model.Ip = jsonModel.data.ip;
14             model.Country = jsonModel.data.country;
15             model.Area = jsonModel.data.area;
16             model.Region = jsonModel.data.region;
17             model.City = jsonModel.data.city;
18             model.County = jsonModel.data.county;
19             model.Isp = jsonModel.data.isp;
20             //插入对象
21             bll.Add(model);
22             context.Response.Write(model.Country + " " + model.Area + " " + model.Region + " " + model.City + " " + model.County + " " +  model.Isp);
23         }

 

 

核心一句:

var jsonModel = JsonConvert.DeserializeObject<dynamic>(ipJson);

此句话是代表程序运行是解析对象。

此时在vs中 通过jsonModel  点是没有提示的,直接根据自己的json格式获取自己需要的属性等

posted @ 2015-04-10 15:06  丑陋的侧面  阅读(3985)  评论(0编辑  收藏  举报