0

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

1
  • no joy gnome ..i changed the code to the following and the result was the same; public JsonResult TopGainers() { IEnumerable<TopGainers> list = db.GetTopGainers(); return Json(list, JsonRequestBehavior.AllowGet); } Commented Dec 9, 2010 at 16:51

2 Answers 2

1

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()});
Sign up to request clarification or add additional context in comments.

Comments

0

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);
    });
});

4 Comments

thanks but that didnt help...i changed the code to use JsonResult but it still gave the same output.. The thing i noticed is each json entry has 'dailygainers' in front of it..that seems to be the main difference..where to i get to state this in code? thanks
I would look at the GetDailyList() method, modify it to return an enumerable list. Maybe change var mkt = db.GetDailyList(); to IEnumerable<dailygainers> list = db.GetDailyList();
no joy gnome ..i changed the code to the following and the result was the same; public JsonResult TopGainers() { IEnumerable<TopGainers> list = db.GetTopGainers(); return Json(list, JsonRequestBehavior.AllowGet); } –
sorry to bring back a dead thread alive. but i kinda did the same thing and to test the data i'm trying to show it on a alert window. the data that it's return is this [object Object] instead of the regular json form 1:test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.