【发布时间】:2014-01-25 02:43:09
【问题描述】:
这有点类似于JSON.net - field is either string or List
假设我有以下 POCO 类,需要将 json 字符串反序列化为该类型。
public class Movie
{
[JsonProperty("title")]
public List<string> Title { get; set; }
[JsonProperty("images")]
public List<Image> Images { get; set; }
[JsonProperty("actors")]
public List<Actor> Actors { get; set; }
[JsonProperty("directors")]
public List<Director> Directors { get; set; }
}
假设需要反序列化的json字符串如下
{
"title": "Movie title",
"images": [
{"url" : "http://www.url.com/img_3.jpg"},
{"url" : "http://www.url.com/img_4.jpg"}
],
"actors": [
{"name" : "Steven Berkoff"},
{"name" : "Nikolaj Coster-Waldau"},
{"name" : "Julie Cox"}
],
"directors": { "name" : "Simon Aeby" }
},
{
"title": "Movie title",
"images": {"url" : "http://www.url.com/img_3.jpg"},
"actors": {"name" : "Julie Cox"},
"directors": { "name" : "Simon Aeby" }
}
这里的问题甚至认为 POCO 需要一个导演、演员和图像的列表,json 每种类型只有一种。尝试反序列化时会出错。
【问题讨论】: