1.获取一个页面的json值,转换为实体类
public static void GetPurchaseMatPriceLogList()
{HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("www.json.com");
request.Timeout = 5000;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("内容类型:" + response.ContentType);
Console.WriteLine("内容长度:" + response.ContentLength);
Console.WriteLine("服务器名:" + response.Server);
Console.WriteLine("资源的URI:" + response.ResponseUri);
Console.WriteLine("HTTP协议版本:" + response.ProtocolVersion);
Console.WriteLine("相应状态:" + response.StatusCode);
Console.WriteLine("相应方法:" + response.Method);
//头信息
for (int i = 0; i < response.Headers.Count; ++i)
{
Console.WriteLine("\nHeader Name:{0},----------Value:{1}", response.Headers.Keys[i], response.Headers[i]);
}
StreamReader sr = new StreamReader(response.GetResponseStream());
string jsonstr = sr.ReadToEnd(); //这是获取到的Json字符串
//开始转换
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<PurchaseMatPriceLogList>));
//jsonstr = "[" + jsonstr + "]";
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonstr));
object ob = serializer.ReadObject(stream);
List<PurchaseMatPriceLogList> ls = (List<PurchaseMatPriceLogList>)ob;
if (ls == null)
{
throw new ArgumentNullException("未获取到最新的数据");
}
//判断每一条数据是不是当天,如果是就更新,不是就添加
foreach (PurchaseMatPriceLogList pl in ls)
{
//暂时只处理板材
if (pl.metel_type_title == "不锈钢板" || pl.metel_type_title == "碳钢板材")
{
PurchaseMatPriceLogApp.AddIUDPurchaseMatPriceLog(pl);
}
}
}
2.将实体类中的值转换为json
- public string getJsonInfo()
- {
- UserInfo userInfo = new UserInfo();
- //userInfo.strNameInfo = "张三";
- //userInfo.intAgeInfo = 23;
- //userInfo.intTelInfo = 66666;
- //userInfo.strAddrInfo = "北京市";
- //userInfo.strPasswordInfo = "yhx.123";
- userInfo.strName = "张三";
- userInfo.intAge = 23;
- userInfo.strPsd = "yhx.123";
- userInfo.intTel = 2324;
- userInfo.strAddr = "北京市";
- //将对象序列化json
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UserInfo));
- //创建存储区为内存流
- System.IO.MemoryStream ms = new MemoryStream();
- //将json字符串写入内存流中
- serializer.WriteObject(ms, userInfo);
- System.IO.StreamReader reader = new StreamReader(ms);
- ms.Position = 0;
- string strRes = reader.ReadToEnd();
- reader.Close();
- ms.Close();
- return strRes;
- }
- /// <summary>
- /// 将json数据转换成实体类
- /// </summary>
- /// <returns></returns>
- private static List<UserInfo> getObjectByJson(string jsonString)
- {
- // 实例化DataContractJsonSerializer对象,需要待序列化的对象类型
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<UserInfo>));
- //把Json传入内存流中保存
- jsonString = "[" + jsonString + "]";
- MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
- // 使用ReadObject方法反序列化成对象
- object ob = serializer.ReadObject(stream);
- List<UserInfo> ls = (List<UserInfo>)ob;
- return ls;
- }
- /// <summary>
- /// 在页面上显示转换为实体类数据
- /// </summary>
- private string disJsonInfo(List<UserInfo> us)
- {
- string strItem = "";
- foreach(var item in us )
- {
- strItem += item.strName + ":" + item.strPsd + ":" + item.intAge + ":" + item.intTel + ":" + item.strAddr + "<br/>";
- }
- return strItem;
- }