On the controller, my code is like:
format.json {render :json=>{ :entities => @entities, :entity_category_counters => @entity_category_counters }}On the Browser side, my code is like:
$.getJSON('/entities.json',function(data){ //data.entities //data[0].entities //data['entities'] });No matter which way I use to access the return JSON object's data, I cannot succeed.
The returned JSON is like:
{"entities":[{"entity":{"created_at":"2011-08-01T18:14:32Z","description":"xxx"}},{"entity":{"created_at":"2011-08-01T18:16:02Z","description":"xxx"}}],"entity_category_counters":[{"entity_category_counter":{"comment_category_id":1,"counter":4,"entity_id":1,"id":1,"important_tag":false}},{"entity_category_counter":{"comment_category_id":2,"counter":0,"entity_id":1,"id":2,"important_tag":false}},{"entity_category_counter":{"comment_category_id":3,"counter":0,"entity_id":1,"id":3,"important_tag":false}},{"entity_category_counter":{"comment_category_id":1,"counter":3,"entity_id":2,"id":4,"important_tag":false}},{"entity_category_counter":{"comment_category_id":2,"counter":0,"entity_id":2,"id":5,"important_tag":false}},{"entity_category_counter":{"comment_category_id":3,"counter":1,"entity_id":2,"id":6,"important_tag":false}}]}
If I display the data directly with Alert like the following:
$.getJSON('/entities.json',function(data){ //data.entities //data[0].entities //data['entities'] alert(data); });
the result in the message box is like:
[object Object],[object Object]
So can anybody tell me how to parse and access data in the returned JSON object that contains two objects? Thanks in advance!