首先,大家要明白什么是json,了解更多关于json方面资料大家可以点击
https://www.ibm.com/developerworks/cn/web/wa-lo-json/
,我在这里简单介绍下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)
这两个方法可以实现基本的序列化和反序列化要求,请看下面的例子:
JsonConvert.DeserializeObject(string value, Type type),反序列化,它有个重载方法 JsonConvert.DeserializeObject(string value, Type type, params JsonConverter[] converters)
这两个方法可以实现基本的序列化和反序列化要求,请看下面的例子:
首先我们先建一个Person类代码如下:
1)序列化
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}
2)反序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
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