简体   繁体   English

使用 jquery 解析以下 json 并在 html 表中显示数据

[英]parse the below json with jquery and display data in a html table

I need to parse the below json with jquery and display in a html table.我需要用 jquery 解析下面的 json 并显示在 html 表中。

display the values of "key" and "doc_count" in a html table.在 html 表中显示“key”和“doc_count”的值。

Help is appreciated.帮助表示赞赏。 see json below:见下面的json:

{ "key": "A", "count": 100 }, { "key": "AB", "count": 800 } { "key": "A", "count": 100 }, { "key": "AB", "count": 800 }

Try This code:试试这个代码:

Use any Template Engine(Handlebars or Underscore) to process HTML.使用任何模板引擎(把手或下划线)来处理 HTML。 I am assuming data is in the form of Static Json.我假设数据是静态 Json 的形式。

Make a table and append to HTML page:制作一个表格并附加到 HTML 页面:

var Table=$("<table class='json_data'>");
$('body').append(Table);

Then iterate whole Json to get the desired value.Like this:然后迭代整个 Json 以获得所需的值。像这样:

$.each(parse_json.aggregations[2].buckets,function(index,value){
   $('.json_data').append("<tr><td>"+value.key+"</td><td>"+value.doc_count+"</td></tr>");
})

JSFiddle Link: https://jsfiddle.net/Dee0565/17u4xs3s/ JSFiddle 链接: https ://jsfiddle.net/Dee0565/17u4xs3s/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM