var json2 = "{'dateAdded':1538207507,'lastModified':1538205295}";
var bookmarks = JsonConvert.DeserializeObject<Test>(json2, new MyDateTimeConverter());
public class Test
{
[JsonProperty(PropertyName = "dateAdded")]
[JsonConverter(typeof(UnixDateTimeConverter))]
public DateTime dateAdded;
[JsonProperty(PropertyName = "lastModified")]
[JsonConverter(typeof(UnixDateTimeConverter))]
public DateTime lastModified;
public Test(DateTime dateAdded, DateTime lastModified)
{
this.dateAdded = dateAdded.ToLocalTime();//当反序列化后时间因为时区的原因少8个小时
this.lastModified = lastModified.ToLocalTime();
}
}
public class MyDateTimeConverter : Newtonsoft.Json.JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTime);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var t = long.Parse((string)reader.Value);
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(t).ToLocalTime();//这里转换了时间,不知道为什么没用
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
var json2 = "{'dateAdded':1538207507,'lastModified':1538205295}"; var bookmarks = JsonConvert.DeserializeObject&lt;Test&gt;(json2, new MyDateTimeConverter()); public class Test {...