本文介绍了如何将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的库将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表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!