我是從,看起來像這樣的HTTP請求接收一個JSON字符串返回:將JSON字符串反序列化爲C#中的嵌套類?
[
{
"size":590,
"location":"California",
"report":{
"Bob":[
null,
"12.0.250.0",
"20130228"
],
"Mary":[
null,
"2013-02-28.01",
"20130228"
],
"John":[
null,
"12.00",
"59123805"
]
}
},
{
"size":12348,
"location":"Florida",
"report":{
"Misty":[
null,
"65492.592",
"89216753"
],
"Billy":[
null,
"789208.w9",
"65320880"
],
"John":[
null,
"89.8056",
"75920889"
]
}
}
]
,我試圖反序列化到類的結構如下所示:
[DataContract]
public class DeserializedObject
{
[DataMember(Name = "size")]
public UInt64 Size { get; set; }
[DataMember(Name = "location")]
public string Location { get; set; }
[DataMember(Name = "report")]
public Dictionary<string, ReportData> Report { get; set; }
}
[CollectionDataContract]
public class ReportData : List<object>
{
public string a
{
get { return (string)this[0]; }
set { this[0] = (string)value; }
}
public string b
{
get { return (string)this[1]; }
set { this[1] = (string)value; }
}
public string c
{
get { return (string)this[2]; }
set { this[2] = (string)value; }
}
}
當我儘管試圖反序列化它,但報告總是空的。
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<DeserializedObject>));
List<DeserializedObject> list = serializer.ReadObject(stream) as List<DeserializedObject>;
什麼是反序列化JSON響應的「報告」部分的正確方法?
它是空的Dictionary對象或純空? – Vlad 2013-03-15 22:05:27
這是一個空的Dictionary對象。 – 2013-03-15 22:14:57