1

i am able to get data from asp.net mvc controller's action (returning json) to jquery in view. I am getting here data collection in 't'. but I need to iterate each collection in data , so I want to get something like t.count . so that i can perform the for loop there.

This is working for me :

       $.getJSON('/LoadTest/GetAllQuestionsForTest', function(data) {

                        var t = $.parseJSON(data);
                        alert(t[0].QuestionText);

    // want here  
    //var count = t.count;//how to get this value ?
   // for(i=1;i <= count;i++)
   // {
    //create div on the fly (this i can do )
   // }


                    });

but showing only 0'th indexed value. I want to get count of objects in data collection. so that i can iterate it. how to do that?

Red Swan
  • 15,157
  • 43
  • 156
  • 238
  • problem solved by var t = $.parseJSON(data); count = t.length; for (j = 1; j <= count; j++) {//code here} – Red Swan Feb 04 '11 at 06:31

3 Answers3

3

I can only assume that the returned JSON is an array of objects. If so then you can get the size of the array in javascript with the 'length' property (instead of 'count', which doesn't exist in js)

Simon
  • 152
  • 1
  • 3
  • 15
1

I'd suggest that first you fully understand the format of the JSon being provided back to you, may be the collection are trying to iterate over is not the root element. You then should probably look into the foreach command in javascript to iterate over your data without the need to determine the count (unless the count is important to you elsewhere). Lastly, there is a good introduction to JSON on MSDN.

Lazarus
  • 41,906
  • 4
  • 43
  • 54
0

Without knowing what the JSON data being returned looks like, I am assuming you could just loop through every item in the collection using a for loop like the one in the answer to this question => How to Loop through plain JavaScript object with objects as members?

However, it'd be helpful if you gave an example of what the JSON data looks like.

Community
  • 1
  • 1
ryanulit
  • 4,983
  • 6
  • 42
  • 66