SO上可能有500个这样的问题,并且有一百万个网站都提供信息花絮-但我就是看不到树木的树林。这似乎应该是令人尴尬的简单,但我就是无法让它发挥作用。
我有一个返回序列化 JSON 对象的 WCF Web 服务:
[OperationContract(Name = "PeopleData"), WebGet(BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "people/{subset}", ResponseFormat = WebMessageFormat.Json)]
PeopleObject GetPeople(string subset);
这可行 - 如果我从浏览器中点击该 URI,则会调用 GetPeople 并返回 JSON 序列化PeopleObject
(此处为保护隐私而编辑的实际数据值):
{"HashValue":"XXXXX","People":[{"EmailAddress":"XXXXX","EmployeeID":99999,"Gender":"X","JobTitle":"XXXXX","Office":"","PreferredName":"XXXXX","Surname":"XXXXX","WorkExtensionNumber":"XXXXX","WorkPhoneNumber":"XXXXX","Department":"XXXXX","DeskNumber":"XXXXX","EmploymentClassification":"XXXXX","InternationalExtensionNumber":"XXXXX","IsFirstAider":false,"Languages":[{"LanguageID":9,"LanguageSkillID":9},{"LanguageID":9,"LanguageSkillID":9}],"QualificationInitials":"XXXXX","QualificationTitle":"XXXXX","Secretaries":null,"WorkMobilePhoneNumber":"XXXXX"}],"RecordCount":"1","SizeBytes":"12345"}
在此示例中,PeopleObject
有效负载仅包含Person
集合中的一个对象,但可能包含许多对象(取决于/{subset}
.
这是类层次结构PeopleObject
- 它是一个顶级容器,包含一些关于有效负载的元数据,以及一个Person
对象列表<>。这些对象又具有一堆简单的类型属性,以及两个进一步嵌套的 List<>Language
和Secretary
对象(可能填充也可能不填充):
[DataContract]
public class PeopleObject
{
[DataMember]
public string HashValue { get; set; }
[DataMember]
public List<Person> People { get; set; }
[DataMember]
public string RecordCount { get; set; }
[DataMember]
public string SizeBytes { get; set; }
}
[DataContract]
public class Person
{
[DataMember]
public string EmailAddress { get; set; }
// <-- snip - lots of fields like this, no point listing them all here
[DataMember]
public bool IsFirstAider { get; set; }
[DataMember]
public List<Language> Languages { get; set; }
[DataMember]
public List<Secretary> Secretaries { get; set; }
}
[DataContract]
public class Language
{
[DataMember]
public int LanguageID { get; set; }
[DataMember]
public int LanguageSkillID { get; set; }
}
[DataContract]
public class Secretary
{
[DataMember]
public int EmployeeID { get; set; }
[DataMember]
public char FirstSurnameLetter { get; set; }
}
到目前为止,一切都很好 - WCF 使用包含所有字段及其内容的 JSON 结构进行响应。现在在客户端应用程序中反序列化该结构(使用相同的类层次结构定义):
// I have a little helper-class to manage the WCF request and return a Stream
using (Stream response = wcfHelper.GetRequestResponseStream(MY_WCF_URI))
{
// This is debug code to prove the response arrives as expected - it does
//StreamReader sr = new StreamReader(response);
//Console.WriteLine("\nResponse:\n{0}", sr.ReadToEnd());
// Deserialise the response
DataContractJsonSerializer dc = new DataContractJsonSerializer(typeof(PeopleObject));
PeopleObject p = (PeopleObject)dc.ReadObject(response);
// The object shows 1 record (in the example) but nothing in the List<>
Console.WriteLine("\nDeserialized records: '{0}' [{1}]", p.RecordCount, p.People.Count);
}
所以这正确地反序列化了容器对象,为我提供了记录计数、哈希值和有效负载大小(以字节为单位)。该对象也有一个 Person 对象的 List<>,但它是 null - JSON 响应中的内容没有通过创建和添加 Person 对象成功地重新水化 List<>。
我错过了什么?我的理解是,这种从 JSON 结构中对 C# 对象层次结构的再水化应该自动发生,所以要么不是这种情况(我需要编写一些代码来实现它),要么是这种情况,但我错过了一些明显的事情。