方式一: 使用于 JavaScriptSerializer类
适用于普通场景, Excel导入导出, 前台传输查询参数直接处理等.html
JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = int.MaxValue; var qp = serializer.Deserialize<QueryP>(queryP); private class QueryP { public string OrgID { get; set; } public string KeyWords { get; set; } } JavaScriptSerializer serializer = new JavaScriptSerializer(); var gridHeaders = serializer.Deserialize<List<grid>>(GridHeaders); /// <summary> /// grid表头 /// </summary> [Serializable] public class grid { public string Text { get; set; } public string DataIndex { get; set; } public string Width { get; set; } public List<grid> Cols { get; set; } public bool Hiden { get; set; } public string xtype { get; set; } public string DataType { get; set; }//列类型 }
方式二: (不推荐使用DataContractJsonSerializer) 功能较多, 仅放一个例子, 不深刻研究, 由于开源第三种方式不只功能多, 性能还强. 数据库
//Person实体中的契约 [DataMember],[DataContract],是使用DataContractJsonSerializer序列化和反序列化必需要加的 //使用DataContractJsonSerializer方式须要引入的命名空间,在System.Runtime.Serialization.dll.中 using System.Runtime.Serialization.Json; static void Main(string[] args) { //序列化操做 Person p1 = new Person() { name = "fxhl", age = 23 }; DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Person)); MemoryStream msObj = new MemoryStream(); //将序列化以后的Json格式数据写入流中 js.WriteObject(msObj, p1); //从0这个位置开始读取流中的数据 msObj.Position = 0; StreamReader sr = new StreamReader(msObj, Encoding.UTF8); string json = sr.ReadToEnd(); Console.WriteLine(json); sr.Close(); msObj.Close(); //反序列化操做 using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) { DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(typeof(Person)); Person model = (Person)deseralizer.ReadObject(ms);// //反序列化ReadObject Console.WriteLine(model.name); } Console.ReadKey(); } } [DataContract] public class Person { [DataMember] public string name { get; set; } [DataMember] public int age { get; set; } }
方式三: 引入 Newtonsoft.Json 使用.
拓展方法使用, 能够直接使用. json
public static T DeserializeModel<T>(this T model, string json) { try { return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json); } catch (Exception) { return model; } } public static T DeserializeModel<T>(this T model, DataTable dt) { try { return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(Newtonsoft.Json.JsonConvert.SerializeObject(dt)); } catch (Exception) { return model; } } public static T DeserializeJSON<T>(this string json) where T : new() { try { return Newtonsoft.Json.JsonConvert.DeserializeObject<T>((json)); } catch (Exception) { return new T(); } } public static string SerializeModel(this object model) { return Newtonsoft.Json.JsonConvert.SerializeObject(model); } public static List<T> NMList<T>(this T model)// where T:new() { var L = new List<T>(); return L; } public static Hashtable NMToHashTable<T>(this T model) { var ht = new Hashtable(); foreach (var f in model.GetType().GetProperties()) { ht[f.Name] = f.GetValue(model, new object[] { }); } return ht; }
支持属性转译 和忽略字段.
JsonIgnore 注解 和 PropertyName 注解等.ide
public class AuditModel { [JsonProperty(PropertyName = "checked", NullValueHandling = NullValueHandling.Ignore)] public string SuccessMsg { get; set; } [JsonIgnore] public HttpRequestBase context { get; set; }
最后, 利用Json.NET 完成Json到Xml 的转换:(完成的xml传输到数据库. 数据库根据/root 取相应执行逻辑)性能
string JsonTOXml(string json, string modelName) { string _json = @"{ ""?xml"": { ""@version"": ""1.0"", ""@standalone"": ""no"" }, ""root"": { ""modelName"": " + json + @" } }"; _json = _json.Replace("modelName", modelName); return Newtonsoft.Json.JsonConvert.DeserializeXmlNode(_json).OuterXml; }
其余优秀博客可参考:
https://www.cnblogs.com/fengxuehuanlin/p/5700282.html
http://www.cnblogs.com/yunfeifei/p/4086014.html
https://blog.csdn.net/WuLex/article/details/83026080
http://www.cnblogs.com/wuhuacong/p/3698122.html
this