Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm having trouble figuring out a way to access the following json returned from my controller:

[{"ID":1,"seatnum":"A1","status":0},{"ID":2,"seatnum":"A2","status":0}]

I am trying to to access the each seatnum value (A1, A2) to perform the following function:

$.ajax({
    url: 'seats/getseats',
    dataType: "JSON",
    error: function (data) {
        $("table tr img").removeClass("available").attr("src", "images/taken.gif");
        alert("error")
    }
}).done(function (data) {


    for (var i = 0; i < data.length; i++) {
        //alert(data[i].seatnum);
        var seatLetter = data[i][0];
        var seatNumber = data[i].substr(1);
        var $target = getImgFromSeat(seatLetter, seatNumber);
        $target.removeClass("available").attr("src", "images/taken.gif");

    }

If it helps my Controller method is:

//
    // GET: /seats/

    public ActionResult Index()
    {
        return View(db.seats.ToString());
    }

    public JsonResult getseats()
    {


        var taken = from b in db.seats
                         select b;

        taken = taken.Where(b => b.status.Equals(0));
        return Json(taken, JsonRequestBehavior.AllowGet);
    }
share|improve this question
up vote 0 down vote accepted

I believe that you are looking for something like:

$.ajax({
  type: "GET",
  url: '@Url.Action("getseats")',
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: successFunc,
  error: errorFunc
});

function successFunc(data, status) {
  for (var i = 0; i < data.length; i++) {
    //alert(data[i].seatnum);
    var seatLetter = data[i][0];
    var seatNumber = data[i].substr(1);
    var $target = getImgFromSeat(seatLetter, seatNumber);
    $target.removeClass("available").attr("src", "images/taken.gif");    
  }
}

function errorFunc() {
  alert('error');
}

The returned json is an array of objects, so instead of accessing it like data[i][0] you should access it like data[i].seatnum, data[i].status, etc.

Additionally I recommend you to use some debugging tools like your browsers developer tools so you can inspect the values of different objects.

PS: do not hard-code the URL value. Instead use @Url.Action("action-name") to generate it.

share|improve this answer
    
I've updated my question to show my ajax request to the get seats action method – Jason Nov 30 '14 at 18:58
    
Please try my approach. Also you did not mention what kind of error are you getting. – Mihai-Andrei Dinculescu Nov 30 '14 at 19:12
    
data[i].seatnum isn't selecting each seatnum only data[0].seatnumetc seems to work – Jason Nov 30 '14 at 19:19
    
Please read my updated answer. – Mihai-Andrei Dinculescu Nov 30 '14 at 19:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.