Newtonsoft.Json
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; } } }
1)序列化 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); } } } 输出结果: {"Name":"GoldenEasy","Age":25}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
{
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);
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。