【发布时间】:2015-10-27 19:23:39
【问题描述】:
[XmlElement("A", Type = typeof(MyModelA)), XmlElement("B", Type = typeof(MyModelB))]
public List<object> context{ get; set; }
可以工作,但我想更改为 JsonProperty ,像这样
[JsonProperty("A", ItemConverterType = typeof(MyModelA)), JsonProperty("B", ItemConverterType = typeof(MyModelB))]
public List<object> context{ get; set; }
失败了,怎么办?
{
node:{
A:{ MyModelA }
B:{ MyModelB }
}
}
【问题讨论】:
-
这是否意味着,如果列表中有多个
MyModelA实例,您希望看到属性名称"A"在 JSON 对象中重复? JSON rfc 表示 对象中的名称应该是唯一的。 所以我不推荐它。 -
Json.NET won't do that out of the box。它将非字典集合序列化为 JSON 数组,而不是对象。此外,它并不特别支持您似乎需要的同一对象中的重复属性名称。如果你真的需要,你可以从How to deserialize JSON with duplicate property names in the same object开始。
-
感谢您的回答,我知道我的问题,所以我尝试另一种方法使其工作。