代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Web.Script.Serialization;
namespace JsonDemo
{
[DataContract]
public class User
{
[DataMember]
public int UserID { get; set; }
[DataMember]
public string UserName { get; set; }
[DataMember]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime CreateTime { get; set; }
[DataMember]
//[JsonConverter(typeof(JavaScriptDateTimeConverter))]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime Birthday { get; set; }
[DataMember]
public List<string> Urls { get; set; }
[ScriptIgnore]
public int Salary{get;set;}
public User() { Urls=new List<string>();}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
namespace JsonDemo
{
class Program
{
static void Main(string[] args)
{
JsonConverterDemo();
}
static void JsonConverterDemo()
{
User user = new User { UserID = 1, UserName = "单车", Salary = 1000, CreateTime = DateTime.Now.AddYears(-2), Birthday = DateTime.Now.AddYears(-23) };
string result = JsonConvert.SerializeObject(user);
Console.WriteLine("序列化后的结果:{0},length:{1}", result, result.Length);
//Debug.WriteLine("112");
user = JsonConvert.DeserializeObject<User>(result);
Console.WriteLine("反序列化后的结果:{0},{1},{2}",user.UserName,user.Birthday,user.CreateTime);
Console.Read();
}
public static void JsonConverterLinqDemo()
{
User user = new User { UserID = 1, UserName = "单车", Salary = 1000, CreateTime = DateTime.Now.AddYears(-2), Birthday = DateTime.Now.AddYears(-2), Urls = new List<string> { "http://zhoufoxcn.blog.51cto.com", "http://blog.csdn.net/zhoufoxcn" } };
string result = JsonConvert.SerializeObject(user);
Console.WriteLine("序列化后的结果:{0},length:{1}", result, result.Length);
//使用Linq to Json
JObject obj = JObject.Parse(result);
JToken jtoken=obj["Urls"];
List<string> slist = new List<string>();
foreach(JToken j in jtoken)
{
slist.Add(j.ToString());
}
Console.WriteLine("反序列化后的结果:");
User usernew = new User();
usernew.UserName=(string)obj["UserName"];
usernew.Birthday = Convert.ToDateTime(obj["Birthday"].ToString());
usernew.CreateTime = Convert.ToDateTime(obj["CreateTime"].ToString());
Console.WriteLine("{0},{1},{2}", usernew.UserName, usernew.Birthday,usernew.CreateTime);
for (int i = 0; i < slist.Count;i++ )
{
Console.Write(slist[i]+" ");
}
}
}
}
注意:User类的两个属性Birthday和CreateTime使用了[JsonConverter(typeof(IsoDateTimeConverter))]标记
使用[JsonConverter(typeof(IsoDateTimeConverter))]和[JsonConverter(typeof(JavaScriptDateTimeConverter))]序列化的结果是不一样的,建议使用[JsonConverter(typeof(IsoDateTimeConverter))]
使用[DataContract]要添加System.Runtime.Serialization引用
测试结果:
------ Test started: Assembly: JsonDemo.exe ------
序列化后的结果:{"UserID":1,"UserName":"单车","CreateTime":"2011-12-04T09:12:49.34375+08:00","Birthday":"2011-12-04T09:12:49.34375+08:00","Urls":["http://zhoufoxcn.blog.51cto.com","http://blog.csdn.net/zhoufoxcn"]},length:196
反序列化后的结果:
单车,2011-12-4 9:12:49,2011-12-4 9:12:49
http://zhoufoxcn.blog.51cto.com http://blog.csdn.net/zhoufoxcn
1 passed, 0 failed, 0 skipped, took 0.88 seconds (Ad hoc).