我們正在使用.Net 4.6構建Web Api應用程序我們正在嘗試JsonConvert.DeserializeObject一個複雜對象。該對象具有複雜對象的列表,並且該對象是一個接口。 因此,我們有一個Task類具有TaskDetails列表和類有IBehaviorJson使用Interface作爲屬性反序列化一個複雜對象
public class Task
{
public int Id { get; set; }
public int TaskTypeId { get; set; } //TYPE
public List<TaskDetail> TaskDetails { get; set; }
}
public class TaskDetail
{
public int Id { get; set; }
public IBehavior Behavior { get; set; }
}
的屬性和IBehavior是一個空的接口...
public interface IBehavior
{
}
和具體行爲.. 。
public class PartPick : IBehavior
{
public bool AllowMultiplePicks { get; set; }
public bool RunLightsOnly { get; set; }
public bool StandardLightMode { get; set; }
}
或者
public class TorqueTool : IBehavior
{
public short PSet { get; set; }
public short RundownsRequired { get; set; }
public int MultiSpindleMask { get; set; }
}
JSON的有效載荷是:
{
"id": 10000,
"name": "Attach Spoiler",
"taskTypeId": 1,
"behavior": {
"pSet": 1,
"rundownsRequired": 1,
"multiSpindleMask": 4,
"multiSpindleMaskString": "0010000000000000"
}
}
我收到以下錯誤:
"The request is invalid.","modelState":{"task.taskDetails[0].behavior.pSet":["Could not create an instance of type Bl.Models.EPA.IBehavior. Type is an interface or abstract class and cannot be instantiated. Path 'taskDetails[0].behavior.pSet'
我試圖創建一個JsonBodyModelbinder:IModelBinder 在反序列化的方法是:
private static T DeserializeObjectFromJson(string json)
{
var binder = new TypeNameSerializationBinder("");
var obj = JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
Binder = binder
});
return obj;
}
它在httpConfiguration中配線:
config.Services.Insert(typeof(ModelBinderProvider), 0,
new SimpleModelBinderProvider(typeof(IBehavior), new JsonBodyModelBinder<IBehavior>()));
我也試着把下面的屬性放在TaskDetail中的IBehavior屬性上。
[JsonConverter(typeof(JsonBodyModelBinder<IBehavior>))]
public IBehavior Behavior { get; set; }
的可能的複製[鑄造界面在JSON.NET反序列化(http://stackoverflow.com/questions/5780888/casting-interfaces-for-deserialization-in-json-淨) – BWA