【发布时间】:2014-11-09 17:50:14
【问题描述】:
ASP.NET MVC4 Web API 控制器应返回包含数字属性名称和值的 json 结果,如
{ "openTimes":{"1":"09:00","2":"09:15","3":"09:30", ... }}
属性名称以 1 开头。属性和属性值的数量是在代码中创建的。 如何返回这样的结果?
我试过控制器
[HttpPost]
public class TestController : ApiController
{
public HttpResponseMessage Post()
{
return Request.CreateResponse(HttpStatusCode.OK, new {
openTimes = new { @"1"="09:00", @"2"="09:15", @"3"="09:30" }
});
}
}
编译错误
Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
列表中的元素数量也是在运行时确定的。它不像此示例中那样固定。 如何在代码中生成属性名称“1”、“2”及其值的可变长度列表。
如何从 Web API 返回这样的列表?
【问题讨论】:
标签: c# asp.net-mvc json asp.net-mvc-4 asp.net-web-api