iterating over nested JSON object..

iterating over nested JSON object..

I have created a JSON object through a PHP script. The code is as follows:
  1. $result1 = array();
  2. $i = 0;
  3. while($row = $result->fetch_assoc())  // $result contains result from a database query
  4. {
  5. $fuser = $row['uname'];
  6. $fid = $row['uid'];
  7. $name = getName($fuser);
  8. $temp = array("name" => $name, "uname" => $fuser);
  9. $result1[$i] = json_encode($temp);
  10. $i = $i+1;
  11. }
  12. return json_encode($result1);

I want to get each json object (represented by $temp) and then value of "name" and "uname" fields for each such objects. But I am not ablle to do so.
I get the value of  json_encode($result1) in variable "result" in Javascript function.

when I do alert(result) I am able to see all the values. But I replace it with : 
 
  1. for(var i=0;i<result.length;i++)
  2. {
  3. var obj = result[i];
  4.       alert(obj.name);
  5. }
i get "undefined".

Following also gives "undefined":

  1. $.each(result, function(index, val){
  2.       alert(val.name);
  3. });
I am not able to understand where I am going wrong...in the PHP code or in the JS code...

Please help...