为Web 2编写了一个简单的函数,该函数返回国家列表。它返回有效的Json格式,但没有数组/对象名称。我有点难以理解这是如何实现的?
以下是我的C#代码:
[Route("Constants/CountryList")]
[HttpGet]
public IHttpActionResult GetCountryList()
{
IEnumerable<ISimpleListEntity> list = new CountryStore().SimpleSortedListByName();
if (list == null || !list.Any())
{
return NotFound();
}
return Ok(list);
}ISimpleListEntity接口代码在这里。
public interface ISimpleListEntity
{
int Id { get; set; }
string Name { get; set; }
}此服务返回以下Json输出(没有对象/数组名称)
[
{
"Id":1,
"Name":"[Select]"
},
{
"Id":4,
"Name":"India"
},
{
"Id":3,
"Name":"Singapore"
},
{
"Id":2,
"Name":"United Arab Emirates"
}
]但是,我很难实现以下Json格式(对象/数组名为“CountryList”)
{
"CountryList":[
{
"Id":1,
"Name":"[Select]"
},
{
"Id":4,
"Name":"India"
},
{
"Id":3,
"Name":"Singapore"
},
{
"Id":2,
"Name":"United Arab Emirates"
}
]
}发布于 2015-11-27 09:51:28
您可以简单地使用匿名类型:
return Ok(new {
CountryList = list
});https://stackoverflow.com/questions/33954555
复制相似问题