Newtonsoft.Json的使用
//序列化 JsonConvert.SerializeObject(object value) //重载方法 JsonConvert.SerializeObject(object value, params JsonConverter[] converters) //反序列化 JsonConvert.DeserializeObject(string value, Type type) //重载方法JsonConvert.DeserializeObject(string value, Type type, params JsonConverter[] converters)
public class Person { private string name; public string Name { get { return name; } set { name = value; } } private int age; public int Age { get { return age; } set { age = value; } } }
序列化
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Newtonsoft.Json; namespace JSONnet { public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Person person = new Person(); person.Name = "GoldenEasy"; person.Age = 25; string strSerializeJSON = JsonConvert.SerializeObject(person); Response.Write(strSerializeJSON); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Newtonsoft.Json; namespace JSONnet { public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Person person = new Person(); person.Name = "GoldenEasy"; person.Age = 25; string strSerializeJSON = JsonConvert.SerializeObject(person); Person user = (Person)JsonConvert.DeserializeObject(strSerializeJSON, typeof(Person)); Response.Write(user.Name); } } }
输出结果为:GoldenEasy
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。