为JSON对象数组命名

God*_*her 3 c# asp.net json asp.net-web-api

我是JSON的新手,我正在尝试使用Asp.net Web Api从Sql Server中的数据库中获取数据.

我的输出json数组是这样的:

[{"f0":9608,"f1":1461,"frbDescField_F56":"Jan","f2":"1461","f3":"179:48"}]

但是Json的输出应该类似于以下代码:

{"restaurants":[{"f0":9608,"f1":1461,"frbDescField_F56":"Jan","f2":"1461","f3":"179:48"}}}

和我的代码是:

public IEnumerable<VI_TimeTotalMontly> Get(int id, string id1)
{
    using (tfmisEntities Entities = new tfmisEntities())
    {
        var result = Entities.VI_TimeTotalMontly.Where(e => e.F0 == id && e.F2 == id1).ToList();
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何更改我的代码?

Nko*_*osi 5

您可以构建一个强类型的匿名对象以匹配所需的输出.IEnumerable<VI_TimeTotalMontly>当您需要对象响应时,您还需要更改操作的返回类型,就像返回集合一样

public IHttpActionResult Get(int id, string id1) {
    using (var Entities = new tfmisEntities()) {
        var restaurants = Entities.VI_TimeTotalMontly
                              .Where(e => e.F0 == id && e.F2 == id1)
                              .ToList();

        var result = new {
            restaurants = restaurants;
        };

        return Ok(result);
    }
}
Run Code Online (Sandbox Code Playgroud)