【发布时间】:2018-08-21 18:40:03
【问题描述】:
我有一种情况,我需要反序列化一个 json 文件,但在某些情况下,一个属性略有不同。这是一个例子:
[
{
"NameProperty": "ex1",
"OtherProperty":"example",
"DifferentProperty": "here is a string value"
},
{
"NameProperty": "ex1",
"OtherProperty":"example",
"DifferentProperty": ["here", "is", "an" "array"]
},
{
"NameProperty": "ex1",
"OtherProperty":"example",
"DifferentProperty": 234 //number
}
]
以下是此类 json 的模型:
抽象类 PropertyBase { }
class StringProperty : PropertyBase
{
public string Value { get; set; }
}
class ArrayProperty : PropertyBase
{
public IList<string> Value { get; set; }
}
class NumberProperty : PropertyBase
{
public double Value { get; set; }
}
class ExampleModel
{
public string NameProperty { get; set; }
public string OtherProperty
{
get; set;
}
public PropertyBase DifferentProperty { get; set; }
}
然后我想将对象列表传递给 asp.net 核心一个控制器函数,例如:
public IActionResult ExampleFunction([FromBody] List<ExampleModel> request){ }
这是一个问题,因为我知道如何反序列化派生类列表,但我不知道如何反序列化具有抽象类型属性的对象。我想编写一个 JsonConvert 类,该类将传递给 asp.net mvc config。
【问题讨论】:
-
因为抽象类没有实例化
标签: c# asp.net-core .net-core json.net