标签:
//序列化 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
标签:
原文地址:http://www.cnblogs.com/colder/p/4315399.html