Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process JSON using jquery?

I have a controller that returns a list of custom linq-to-sql model objects in JSON format to jquery ajax calls:

List<MyAppLibrary.Model.Search> listSearches = search.ToList();
        return new JsonResult { Data = listSearches };

I have the following javascript which gets the response:

$.getJSON("/ajax/getbrands",
    function(data) {
        alert(data);
    });

I'd like to know how I can process that data response in javascript? How do I get the Name paramter of the Model.Search object?

Thanks.

like image 484
rksprst Avatar asked Jul 31 '09 02:07

rksprst


1 Answers

The data variable that gets returned from the jQuery AJAX call contains the JSON object. You can access the fields of each of your MyAppLibrary.Model.Search objects in your JavaScript like so:

// this will grab the Search object at index 0 of your list
// and put the Name property's value of the Search object
// into a var
var firstItemName = data.Data[0].Name;
like image 70
Tim S. Van Haren Avatar answered Sep 22 '22 18:09

Tim S. Van Haren