1

I have this array which is retrieved by a json_encode().When I execute

$.getJSON('beta.php' , function(data){
    console.log(data);
});

I get the result as follows

[
Object { StuId="1", fName="Saman", more...},
Object { StuId="2", fName="Marry", more...},
Object { StuId="3", fName="Navjoth", more...},
Object { StuId="4", fName="Jassu", more...}
]

I'm trying to iterate through this result using

$.each(data, function(key, value){

            for(var key in value){

                if(value.hasOwnProperty(key)){                  

                        $("#article tbody").html(                       
                        "<tr><td>" + value.StuId + 
                        "</td><td>" + value.fName + 
                        "</td><td>" + value.lName +
                        "</td><td>" + value.age +
                        "</td><td>" + value.grade + 
                        "</td></tr>");

                        $("article tbody").appendTo("document.body");               

                }

                }

        });

.I guess It is impossible because of the above format of the array.

If someone could explain why this is happening and how to correct it I would be really grateful.I want to know how to convert the above into the following format.

[
{"StuId":"1","fName":"Saman","lName":"Kumara","age":"14","grade":"A"},
{"StuId":"2","fName":"Marry","lName":"Vass","age":"12","grade":"B"},
{"StuId":"3","fName":"Navjoth","lName":"Bogal","age":"32","grade":"A"},
{"StuId":"4","fName":"Jassu","lName":"Singh","age":"22","grade":"E"}
]
Eimantas
  • 48,927
  • 17
  • 132
  • 168
SriniShine
  • 1,089
  • 5
  • 26
  • 46
  • you already asked similar questions here :http://stackoverflow.com/questions/6186339/iterating-and-displaying-json-data-with-jquery – xkeshav Jun 02 '11 at 06:15
  • you have already getting the desired output u want where the hell is problem?? `[]` := Array `{}` :=Object – xkeshav Jun 02 '11 at 06:28
  • I'm extremely sorry.but still I couldn't get what I want.that's why I asked it again.BTW I'm new to this whole jQuery subject. – SriniShine Jun 02 '11 at 06:43
  • then its better to learn by yourself; ( yahan tumhare q'n ka solution to mil jaega but tum kabhi seekh nahin paogi, sachhi! ) – xkeshav Jun 02 '11 at 06:49
  • 1
    @diEcho, of course I will get my answer here and also I will learn somehow.sachhii – SriniShine Jun 02 '11 at 06:54

2 Answers2

2

You are overwriting the tables html with different values each time.

$(document).ready(function() {
  var html = "";
  var data = [
          { StuId:"1", fName:"Saman"},
          { StuId:"2", fName:"Marry"},
          { StuId:"3", fName:"Navjoth"},
          { StuId:"4", fName:"Jassu"}
          ]
  $.each(data, function(key, value){
    console.log(value + "--" + key);
    html += "<div><span>" + value.StuId + "</span><span>" + value.fName + "</span></div>";
  });
  $('body').html(html)
});

Loops in JavaScript are different from other languages. You need closures.

Have a look here:

http://www.mennovanslooten.nl/blog/post/62

Christian
  • 1,872
  • 1
  • 14
  • 14
0

The base object is a javascript array, so can you not:

$.each(data, function(item) {
    var html = "<tr><td>" + item.StuId + "</td><td>" + item.fname + "</td></tr>";
    // etc.
});

?

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
  • I have tried this already. but this output a table with only one row and it prints "undefined" for all the values.This is why I used for in loop after reading an post in this forum. It doesn't even print all the rows in the table.also I can't figure out why the looping doesn't happen properly :( – SriniShine Jun 02 '11 at 06:50