[英]Newtonsoft JSON Deserialize Interface
I have a problem with deserializing the following sample JSON snippet: 我反序列化以下示例JSON代码段时遇到问题:
{
"list":{
"A":{"ClassA":{"PropertyA":"test"}},
"B":{"ClassB":{"PropertyB":"test"}}
}
}
With my ClassA: 使用我的ClassA:
public class ClassA:IInterface{
public String PropertyA{get; set;}
}
public class ClassB:IInterface{
public String PropertyB{get; set;}
}
I want the JsonConverter to create a ClassA or ClassB with the PropertyA or PropertyB from the JSON. 我希望JsonConverter用JSON中的PropertyA或PropertyB创建一个ClassA或ClassB。 The ClassA or ClassB is embedded in a Dictionary.
ClassA或ClassB嵌入在Dictionary中。
I can also put the full class name with namespace there but I wasn't really able to make it work. 我也可以将完整的类名和名称空间放在这里,但是我真的无法使其工作。
Thanks in advance for any help 预先感谢您的任何帮助
You need a wrapper class: 您需要一个包装器类:
public class ListClass
{
public WrapperList List { get; set; }
}
public class WrapperList
{
public WrapperA A { get; set; }
public WrapperB B { get; set; }
}
public class WrapperA
{
public ClassA ClassA { get; set; }
}
public class WrapperB
{
public ClassB ClassB { get; set; }
}
public class ClassA : IInterface
{
public String PropertyA { get; set; }
}
public class ClassB : IInterface
{
public String PropertyB { get; set; }
}
Then: 然后:
var list = JsonConvert.DeserializeObject<ListClass>(json);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.