考虑以下给出的json字符串:
{
"items":
[
{
...,
"view_count":4,
"answer_count":0,
"score":0,
"last_activity_date":1445071150
}
],
...
}
是否可以使用last_activity_date
和System.Runtime.Serialization.DataContractAttribute
将System.Runtime.Serialization.Json.DataContractJsonSerializer
反序列化为另一种类型的实例?
例如:1445071150 - > Date
其中Date
是
[System.Runtime.Serialization.DataContract]
public class Date
{
[System.Runtime.Serialization.IgnoreDataMember]
public long ElapsedSeconds { get; set; }
[System.Runtime.Serialization.IgnoreDataMember]
public DateTime Date { get { ... } set; }
public Date(long seconds)
{
ElapsedSeconds = seconds;
Date = ...;
}
public static implicit operator Date(long seconds)
{
return new Date(seconds);
}
// Other members ...
}
答案 0 :(得分:2)
你可以有这样的定义:
[DataContract]
public class Item
{
[DataMember(Name = "view_count")]
public int ViewCount { get; set; }
[DataMember(Name = "answer_count")]
public int AnswerCount { get; set; }
[DataMember(Name = "score")]
public int Score { get; set; }
[IgnoreDataMember]
public Date LastActivityDate { get; private set; }
[DataMember(Name = "last_activity_date")]
private long Date
{
set
{
LastActivityDate = value;
}
get
{
return LastActivityDate.ElapsedSeconds;
}
}
}
现在,当反序列化JSON时,Date属性的setter将负责设置LastActivityDate成员。
这里有一个完整的例子:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
[DataContract]
public class Root
{
[DataMember(Name = "items")]
public Item[] Items { get; set; }
}
[DataContract]
public class Item
{
[DataMember(Name = "view_count")]
public int ViewCount { get; set; }
[DataMember(Name = "answer_count")]
public int AnswerCount { get; set; }
[DataMember(Name = "score")]
public int Score { get; set; }
[IgnoreDataMember]
public Date LastActivityDate { get; private set; }
[DataMember(Name = "last_activity_date")]
private long Date
{
set
{
LastActivityDate = value;
}
get
{
return LastActivityDate.ElapsedSeconds;
}
}
}
[DataContract]
public class Date
{
[IgnoreDataMember]
public long ElapsedSeconds { get; set; }
public Date(long seconds)
{
ElapsedSeconds = seconds;
}
public static implicit operator Date(long seconds)
{
return new Date(seconds);
}
}
static class Program
{
static void Main()
{
string json =
@"{
""items"":
[
{
""view_count"":4,
""answer_count"":0,
""score"":0,
""last_activity_date"":1445071150
}
]
}";
var serializer = new DataContractJsonSerializer(typeof(Root));
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
var root = (Root)serializer.ReadObject(stream);
Console.WriteLine(root.Items[0].LastActivityDate.ElapsedSeconds);
}
}
}