iterating over nested JSON object..
I have created a JSON object through a PHP script. The code is as follows:
- $result1 = array();
- $i = 0;
- while($row = $result->fetch_assoc()) // $result contains result from a database query
- {
- $fuser = $row['uname'];
- $fid = $row['uid'];
- $name = getName($fuser);
-
- $temp = array("name" => $name, "uname" => $fuser);
- $result1[$i] = json_encode($temp);
- $i = $i+1;
- }
-
- 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 :
-
for(var i=0;i<result.length;i++)
-
{
-
var obj = result[i];
-
alert(obj.name);
-
}
i get "undefined".
Following also gives "undefined":
-
$.each(result, function(index, val){
-
-
I am not able to understand where I am going wrong...in the PHP code or in the JS code...
Please help...