我需要用jquery解析下面的json并在html表中显示
在html表格中显示key
和count
的值。输出应该如下所示。
{
"key": "A",
"count": 100
},
{
"key": "AB",
"count": 800
}
非常感谢任何帮助!
答案 0 :(得分:4)
尝试以下方法:
var table = $("table");
var json = '{"took":32,"timed_out":false,"aggregations":{"2":{"doc_count_error_upper_bound":0,"sum_other_doc_count":447529,"buckets":[{"3":{"doc_count_error_upper_bound":2804,"sum_other_doc_count":152552,"buckets":[{"key":"d4","doc_count":6882},{"key":"r3","doc_count":6494}]},"rootkey":"AAA","doc_count":165928},{"3":{"doc_count_error_upper_bound":1574,"sum_other_doc_count":82914,"buckets":[{"key":"in","doc_count":4289},{"key":"d3","doc_count":3516}]},"rootkey":"BBB","doc_count":90719}]}}}';
json = $.parseJSON(json);
$.each(json.aggregations["2"].buckets, function(i, n){
var rootkey = n.rootkey;
n = n["3"];
$.each(n.buckets, function(e, r){
table.append("<tr><td>"+rootkey+"</td><td>"+r.key+"</td><td>"+r.doc_count+"</td></tr>");
});
});