Search code examples
jqueryjsonodata

Get Results array and count from json responce


I got the following json responce from my odata service:

{"$id":"1","Results":[{"$id":"2","ProductID":1,"ProductName":"Chai","UnitPrice":18.0000},{"$id":"3","ProductID":2,"ProductName":"Chang","UnitPrice":19.0000}],"__count":77}

I have written the following jquery code to get the data but not working

      $.getJSON(url, function (data) {
            $("#products").remove();
            var html = '<div id="products" class="row list-group">';

            for (var i = 0;i<data.length;i++)
            {
                html += '<div class="item ' + $("#CurrentView").val() + ' col-xs-4 col-lg-4"><div class="thumbnail"><img class="group list-group-image" src="../img/product2.jpg" alt="" /><div class="caption"><h4 class="group inner list-group-item-heading">';
                html += data[i].ProductName + '</h4><p class="group inner list-group-item-text"> Product description... </p><div class="row"><div class="col-xs-12 col-md-6"><p class="lead">';
                html += data[i].UnitPrice + '</p></div><div class="col-xs-12 col-md-6"><a class="btn btn-success" href="http://www.jquery2dotnet.com">Add to cart</a></div></div></div></div></div>';
              }
            html+= '</div>';
            $(".clear").before(html);
        });

how can i refer to Results array and __count property in jquery?


Solution

  • It should be data.Results.length and data.Results[i].UnitPrice.

    data is an object, and Results (the array) is a property of that object. So data.Results will get you the array itself.