C# JSON帮助类(可互转)
2020-12-13 15:51
阅读:514
标签:style blog io color ar sp for div on
public class JsonHelper { public JsonHelper() { // // TODO: Add constructor logic here // } ////// 把对象序列化 JSON 字符串 /// /// 对象类型 /// 对象实体 /// JSON字符串 public static string GetJson (T obj) { //记住 添加引用 System.ServiceModel.Web /** * 如果不添加上面的引用,System.Runtime.Serialization.Json; Json是出不来的哦 * */ DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream()) { json.WriteObject(ms, obj); string szJson = Encoding.UTF8.GetString(ms.ToArray()); return szJson; } } /// /// 把JSON字符串还原为对象 /// /// 对象类型 /// JSON字符串 /// 对象实体 public static T ParseFormJson (string szJson) { T obj = Activator.CreateInstance (); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson))) { DataContractJsonSerializer dcj = new DataContractJsonSerializer(typeof(T)); return (T)dcj.ReadObject(ms); } } }
//因为在webservice中是不能接收集合的 所以用字符串接收选择了json. //调用:ListExdetailModel ExdetailModel = UPED.JsonHelper.ParseFormJson //解析JSON串为类 //实体类转换JSON串为类 List>(jsonStr);
detailModel =servers.GetExEntryDetailModel(......); string jsonStr = UPED.JsonHelper.GetJson >(detailModel);
C# JSON帮助类(可互转)
标签:style blog io color ar sp for div on
原文地址:http://www.cnblogs.com/ilookbo/p/4081622.html
评论
亲,登录后才可以留言!