在控制器上,我的代码如下:
format.json {render :json=>{ :entities => @entities, :entity_category_counters => @entity_category_counters }}
在浏览器端,我的代码如下:
$.getJSON('/entities.json',function(data){ //data.entities //data[0].entities //data['entities'] });
无论我使用哪种方式访问返回的 JSON 对象的数据,我都无法成功。
返回的 JSON 如下:
{"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,"重要标签“:假}}]}entity_id":2,"id":6,"important_tag":false}}]}entity_id":2,"id":6,"important_tag":false}}]}
如果我直接使用 Alert 显示数据,如下所示:
$.getJSON('/entities.json',function(data){ //data.entities //data[0].entities //data['entities'] alert(data); });
消息框中的结果如下:
[object Object],[object Object]
那么谁能告诉我如何解析和访问包含两个对象的返回 JSON 对象中的数据?提前致谢!