我是JSON的新手,我正在尝试使用Asp.net Web Api从Sql Server中的数据库中获取数据。
My Output json Array就像这样:
[{" f0":9608," f1":1461," frbDescField_F56":" Jan"," f2& #34;:" 1461", " f3":" 179:48"}]
但是Json的输出应该类似于以下代码:
{" restaurant":[{ " f0":9608," f1":1461," frbDescField_F56":" Jan"," f2":& #34; 1461"," f3":" 179:48"}]}
和我的代码是:
string(11) " success: 1"
如何更改我的代码?
答案 0 :(得分: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);
}
}
答案 1 :(得分:1)
我会使用dynamic
对象来包装你的结果:
dynamic output = new ExpandoObject();
output.restaurants = result;
return output;