【发布时间】:2013-12-19 02:10:37
【问题描述】:
我在 MVC5 中绑定键值对对象数组时遇到问题。这是 JSON:
{
"expiration": "2013-12-03T04:30:41.206Z",
"conditions": [
{
"acl": "private"
},
{
"bucket": "ryvus.upload.test"
},
{
"Content-Type": "application/vnd.ms-excel"
},
{
"success_action_status": "200"
},
{
"key": "fc42ae8a-1f6e-4955-a669-8272ad650cb9.csv"
},
{
"x-amz-meta-qqfilename": "simpleupload.csv"
}
]
}
如果我尝试像这样将 条件 绑定到 Dictionary<string, string>:
// View Model
public class ResponseVM
{
public DateTime Expiration { get; set; }
public Dictionary<string, string> Conditions { get; set; }
}
// Controller Action
public ActionResult MyHandler(ResponseVM s3Response)
{
//do something
return JSON(null);
}
我得到了 6 个条目,其中键为“0”、“1”、“2”、“3”、“4”、“5”和空值。我看起来很接近,但我尝试了很多不同的类型都没有成功,Dictionary<string, string> 是我能得到的最好的。
我在这里遗漏了一些完全明显的东西吗?
【问题讨论】:
-
可以修改您的 JSON 架构吗?
-
conditionsJSON 不适合字典的预期架构,因此您需要修改 JSON 架构或自定义反序列化 -
不,很遗憾,我无法更改 JSON 架构,但我可以更改视图模型。
-
您能改为更改
conditions数据类型吗?它真的应该是一本字典吗? -
嘿,这正是我的问题,什么类型会映射到这个 JSON?我尝试了一堆不同的类型都没有成功。
标签: c# asp.net-mvc json asp.net-mvc-5