如何格式化 ASP.NET MVC 2 的 JSON 输出

发布于 2024-10-07 04:27:56 字数 693 浏览 7 评论 0原文

我使用下面的代码生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒尘 2024-10-14 04:27:56

但对于其他偶然发现这个问题的人,我必须做类似的事情,并且我使用 Linq 来做到这一点。

所以你将新的 var 命名为容器的名称,它应该可以做到!

var formatedList = (new {dailygainers = db.GetDailyList()});

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!

var formatedList = (new {dailygainers = db.GetDailyList()});
梓梦 2024-10-14 04:27:56

将操作更改为 JsonResult。我使用了我过去使用过的这个片段。请记住,我使用它来动态填充下拉列表。

HomeController

[HttpGet]
public JsonResult GetSubjects(string term)
{
    IEnumerable<Textbook> subjects = _repository.GetSubjects(term);
    return Json(subjects, JsonRequestBehavior.AllowGet);
}

查看

<p>
<label for="Subject">Subject</label>
<select name="Subject" id="Subject"></select>
</p>

jQuery

$("#Term").change(function () {
    var term = $("#Term > option:selected").attr("value");
    var items = "<option>Select</option>";
    $.getJSON(host + "/Home/GetSubjects/" + term, function (data) {
        $.each(data, function (i, d) {
            items += "<option value='" + d.Subject + "'>" + d.Subject + "</option>";
        });
        $("#Subject").html(items);
    });
});

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

[HttpGet]
public JsonResult GetSubjects(string term)
{
    IEnumerable<Textbook> subjects = _repository.GetSubjects(term);
    return Json(subjects, JsonRequestBehavior.AllowGet);
}

View

<p>
<label for="Subject">Subject</label>
<select name="Subject" id="Subject"></select>
</p>

jQuery

$("#Term").change(function () {
    var term = $("#Term > option:selected").attr("value");
    var items = "<option>Select</option>";
    $.getJSON(host + "/Home/GetSubjects/" + term, function (data) {
        $.each(data, function (i, d) {
            items += "<option value='" + d.Subject + "'>" + d.Subject + "</option>";
        });
        $("#Subject").html(items);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文