gpt4 book ai didi

javascript - 将 JSON 数据以表格格式附加到 HTML div 元素

转载 作者:行者123 更新时间:2023-12-01 01:55:57 25 4
gpt4 key购买 nike

我有一个 JSON 格式

{
"0": "The keys on a keyboard are only clipped on ",
"1": " Make sure the keyboard/mouse is firmly plugged into ...",
"2": "Try restarting your computer. Wait a few seconds and the...",
"3": "Some string..."
}

我想将其转换为 HTML 表格。如下图所示。转换应该在 javascript 中实现。 不是Python

enter image description here

编辑:我尝试使用

附加到 html
        var result;

$("#btn").click(function() {

console.log($("#query").val())
var result = $.ajax({
type: "POST",
url: eva_url,
data:JSON.stringify({
"query": $("#query").val(),
}),
success: function(data) {
console.log(data)
$('#output').html('');
$("#output").append(data[0]);
$("#output").append(data[1]);
$("#output").append(data[2]);
$("#output").append(data[3]);

但是,字符串被连接为一个大段落,这并不是预期的。

最佳答案

你可以试试这个

$(document).ready(function() {

var p = {
"0": "The keys on a keyboard are only clipped on ",
"1": " Make sure the keyboard/mouse is firmly plugged into ...",
"2": "Try restarting your computer. Wait a few seconds and the...",
"3": "Some string..."
};

for (var key in p) {
if (p.hasOwnProperty(key)) {
$(".toAdd").append("<tr><td class='grey'>" + key + "</td><td>" + p[key] + "</td><tr/>");
}
}

});
td,
th {
border: 1px solid black;
text-align: center;
}

.grey {
background-color: #C0C0C0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="toAdd">
<tr>
<th class="grey">Column 1</th>
<th class="grey">Column 2</th>
<tr/>
</table>

关于javascript - 将 JSON 数据以表格格式附加到 HTML div 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51037177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com