如何格式化 ASP.NET MVC 2 的 JSON 输出
我使用下面的代码生成 JSON 数据。
[HttpGet]
public ActionResult MarketList()
{
var mkt = db.GetDailyList();
return Json(mkt, JsonRequestBehavior.AllowGet);
}
现在它返回以下格式的 JSON;
[{"gainers":"CAP","date":"Dec 9 ","Open":30.87,"Close":32.41,"change":1.54},{"gainers":"AP","date":"Dec 9 ","Open":20.00,"Close":21.00,"change":1.00}]
我想要的是返回以下 JSON 格式的数据;
[{"dailygainers":{"gainers":"CAP","date":"Dec 9 ","Open":30.87,"Close":32.41,"change":1.54}},{"dailygainers":{"gainers":"AP","date":"Dec 9 ","Open":20.00,"Close":21.00,"change":1.00}}]
请问我该如何使用 MVC 2 来做到这一点?
谢谢
i used the code below to generate JSON Data.
[HttpGet]
public ActionResult MarketList()
{
var mkt = db.GetDailyList();
return Json(mkt, JsonRequestBehavior.AllowGet);
}
Now it returns JSON in the format below;
[{"gainers":"CAP","date":"Dec 9 ","Open":30.87,"Close":32.41,"change":1.54},{"gainers":"AP","date":"Dec 9 ","Open":20.00,"Close":21.00,"change":1.00}]
what i want is to return data in the following JSON Format;
[{"dailygainers":{"gainers":"CAP","date":"Dec 9 ","Open":30.87,"Close":32.41,"change":1.54}},{"dailygainers":{"gainers":"AP","date":"Dec 9 ","Open":20.00,"Close":21.00,"change":1.00}}]
How can i do this using MVC 2 please?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
但对于其他偶然发现这个问题的人,我必须做类似的事情,并且我使用 Linq 来做到这一点。
所以你将新的 var 命名为容器的名称,它应该可以做到!
but for anyone else who stumbles on this problem I had to do something similar and I used Linq to do it.
So you name the new var the name of the container and it should do it!
将操作更改为 JsonResult。我使用了我过去使用过的这个片段。请记住,我使用它来动态填充下拉列表。
HomeController
查看
jQuery
Change Action to JsonResult. I use this snippet of what I've used in the past. Keep in mind I used this to dynamically populate a drop down list.
HomeController
View
jQuery