如何将 JSON 对象转换为 HTML 表格? [英] How do I convert a JSON Object into a HTML table?

查看:61
本文介绍了如何将 JSON 对象转换为 HTML 表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的 JSON 对象:

I've got a JSON object that looks like this:

{"a": 1, "b": 3, "ds": 4}

我想将其转换为如下所示的 HTML 表格:

I'd like to convert it into a HTML table that looks like this:

name | Value
a     1
b     3
ds    4

谁能告诉我如何实现这一目标?

Could anyone tell me how to achieve this?

推荐答案

使用 jQuery 非常简单:

It's very simple with jQuery:

$(function() {
  var jsonObj = $.parseJSON('{"a":1,"b":3,"ds":4}');
  var html = '<table border="1">';
  $.each(jsonObj, function(key, value) {
    html += '<tr>';
    html += '<td>' + key + '</td>';
    html += '<td>' + value + '</td>';
    html += '</tr>';
  });
  html += '</table>';
  $('div').html(html);
});

这是工作小提琴的链接.

更新:另一种方法是使用一个名为 dynatable 的库a> 将 JSON 转换为可排序的表.

UPDATE: an alternative way to achieve this is by using a library called dynatable to convert the JSON into a sortable table.

这篇关于如何将 JSON 对象转换为 HTML 表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆