数组返回[Object object]而不是value? [重复]

Possible Duplicate:
Json Returning [object object] instead of array

I have an array that contains a sub-array, being pulled via a POST request to a PHP file. The thing is, I am having a hard time finding how to return the value of the array objects, let alone iterate through it.

Javascript

$.ajax({
    type: 'POST',
    url: 'php.php',
    data: 'id=testdata',
    dataType: 'json',
    cache: false,
    success: function(result) {
        alert(result[0]); // returns [Object object]
    },
});

I am sure that it is properly grabbing the array from the PHP file.

Here's the PHP file.

You can iterate over it like:

$.each(result, function () {
    alert(this.name);   // or this.id, this.description, etc.
});

I recommend using a tool such as Firebug, then doing console.log(result); to get a more descriptive output.

In your case, result is an array of objects, so result[0] is the first object. To access the id for example, you would use result[0]['id']. In order to iterate through the array of objects, you can use a for loop like this for example:

for(var i=0, len = result.length; i<len; i++) {
    //write your code for each object in the results here
    var id = result[i]['id'];
}

You must specify in your object, wich one to show, i mean....

alert(result[0]['field_name']);

As you are using PHP to fetch some data from one 'id', I could advice you that when quering mysql, use the next syntax,

echo json_encode(mysql_fetch_object(mysql_query('your_query')));

With this syntax you will get only one object with all the fields fetched in your query, so the alert would be:

alert(result['field_name']);

I hope, this helps.