最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - loop through each JSON response returns "[Object Object]" - Stack Overflow

programmeradmin2浏览0评论

I have this on success AJAX function

success: function(response){
    console.log(response);
    if(response.success){
        $.each(response.vote, function(index, value){
            alert(value);
        });
    }   
}

and this the JSON response from the console (refer to the image below)

but it throws me "[Object Object]" from the alert prompt, any ideas, clues, help, suggestions, remendations?

I have this on success AJAX function

success: function(response){
    console.log(response);
    if(response.success){
        $.each(response.vote, function(index, value){
            alert(value);
        });
    }   
}

and this the JSON response from the console (refer to the image below)

but it throws me "[Object Object]" from the alert prompt, any ideas, clues, help, suggestions, remendations?

Share Improve this question asked Nov 8, 2015 at 10:51 Juliver GalletoJuliver Galleto 9,04727 gold badges91 silver badges168 bronze badges 4
  • Each of the value is an object, alert will make it a string, which impies a .toString on value, so you gets [Object Object]. You can try alert({}) which gives you the same result. If you just want to see that key-value pairs, you can make it a json again, alert(JSON.stringify(value)), but if you just want to access it's values, use value.branch... etc is ok. – fuyushimoya Commented Nov 8, 2015 at 10:54
  • so any ideas how to render it like a string in the alert prompt? like if im going to display the branch, i get each branch. – Juliver Galleto Commented Nov 8, 2015 at 10:57
  • alert casts the objects to string, The toString method on the GrandParent Object class is called using prototype-chain, so it alerts [object Object]. Use console.log. – Tushar Commented Nov 8, 2015 at 10:58
  • Here's my answer to a related issue on trying to alert objects. – Christofer Eliasson Commented Nov 8, 2015 at 11:01
Add a ment  | 

4 Answers 4

Reset to default 3

Do not use alert but console.log instead. You will be able to look into all the objects that way and avoid getting spammed.

Also, if you need to look into a deep object, you can use something like https://github./WebReflection/circular-json which will allow to print even objects that references themselves (circular reference would fail to print big object).

alert uses the object's toString method, which will return this [Object Object] thing. If you want to print an object nicely, you can use JSON.stringify(yourObject)

In you current code, the value is an object, however, alert can only display string, so it'll use .toString to convert your value to a string, which then bees "[Object Object]".

To display the value as key-value pairs use JSON.stringify(value) to make it a json again:

success: function(response){
    console.log(response);
    if(response.success){
        $.each(response.vote, function(index, value){
            alert(JSON.stringify(value));
        });
    }   
}

if you just want to access the attributes of the value, use their key should work:

success: function(response){
    console.log(response);
    if(response.success){
        $.each(response.vote, function(index, value){
            // This will alert each items' `bundle` value.
            // It's enough in your case, but you may have to check if the target attribute you want to alert is also an object.
            alert(value.bundle);
        });
    }   
}

If you want to alert that value than use alert(JSON.stringify(value)).

发布评论

评论列表(0)

  1. 暂无评论