2
我遇到麻烦默认模型绑定到如预期的数据结构如下工作:ModelBinder的反序列化JSON任意
JSON:
{
template: "path/to/template",
slides: [{
index: 0,
context: {
foo: "bar"
}
}, {
index: 1,
context: {
foo: 'bar!',
table: [
['Price', 'Revenue', 'Profit'],
[$5', 100, 20],
['$10', 320, 4],
['$7', 50, 2]
]
}
}]
}
IPresentationData
型号:
public interface IPresentationData
{
public string Template { get; set; }
public ICollection<SlideData> Slides { get; set; }
}
ISlideData
型号:
public interface ISlideData
{
public int Index { get; set; }
public IContext Context { get; set; }
}
IContext
模型:
public interface IContext : IDictionary<string, dynamic>
{
}
默认模型粘合剂正常工作与的SlideData.Context
除外,它可以是任意大的/深对象。
例如,json.context.table
反序列化到:
{
'table[0][0]': 'Price',
'table[0][1]': 'Revenue',
...
}
相反的:
{
'table': [
[...]
]
}
我想我需要重写System.Web.Mvc.DefaultModelBinder#BindModel
只是在SlideData.Context
属性,但我真的不确定从哪里开始。
这里任何帮助将非常感激。
你见过[this site](http://json2csharp.com/)吗? – 2014-08-30 00:13:37
这是相当整洁,但上下文属性是完全动态的 – 2014-08-30 00:18:26
然后你可以选择使用'动态'与Json.Net。只是搜索它。 – 2014-08-30 00:21:28