gpt4 book ai didi

javascript - 如何在表格中显示json字符串的多维数组

转载 作者:行者123 更新时间:2023-11-29 21:44:59 25 4
gpt4 key购买 nike

我通过 ajax 传递从数据库中获取的数据以显示在 html 页面中。我设法从 php 脚本将它传递到一个多维数组中。现在我得到了 json 字符串,但我不确定如何在 html 表中正确显示输出。

脚本

$("#submit").on("click",function()
{

$("#set_setting").submit(function(){

data = $(this).serialize()
$.ajax({
type: "POST",
dataType: "html",
url: "submit_setting.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
alert(data);
//hide the form
$("#set_setting").slideUp("slow");
//show the result
/* for (i = 0; i < data.length; i++) {
console.log(data);
$(".the-return").html(data);
}*/

console.log(data);
$(".the-return").html(data);

}

});
return false;

});

});

PHP脚本

$json=array();
array_push($json,array("type"=>$carType,"maker"=>$carMaker));
echo json_encode($json);

数据输出

HondaHonda maker[{"type":["flying car","3 wheleer","weird car","miracle car","tata car","see car","star car","mn car","jkcar","car test","ting","Honda"],"maker":["test maker","diamond car","ruby car","dont know car","titi car","saw car","moon car","ty car","ty car","car maker test","ting maker","Honda maker"]}]

如何在 html 表格中显示这个 json 字符串?

最佳答案

这里有一些普通的 JS 可以解决问题。可能值得一提的是,您正在返回一个结果数组,其中只有一个元素。这就是我的代码中出现 parsedResult[0] 的原因。如果你想返回多个 html 表的数据,你将有 parsedResult[1]、parsedResult[2] 等。这段代码没有考虑到这种情况——我已经硬编码它以使用只是第一个。

结果:(请原谅 html 表格的文本格式)

type    maker
flying car test maker
3 wheleer diamond car
weird car ruby car
miracle car dont know car
tata car titi car
see car saw car
star car moon car
mn car ty car
jkcar ty car
car test car maker test
ting ting maker
Honda Honda maker

代码:

<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
function newEl(tag){return document.createElement(tag);}

window.addEventListener('load', onDocLoaded, false);

var dummyResultString = '[{"type":["flying car","3 wheleer","weird car","miracle car","tata car","see car","star car","mn car","jkcar","car test","ting","Honda"],"maker":["test maker","diamond car","ruby car","dont know car","titi car","saw car","moon car","ty car","ty car","car maker test","ting maker","Honda maker"]}]';

function onDocLoaded()
{
var ajaxResult = dummyResultString;
var tblOfResults = parseAndCreateTable( ajaxResult );
document.body.appendChild(tblOfResults);
}

function parseAndCreateTable(inputString)
{
var rawResult = inputString;
var parsedResult = JSON.parse(rawResult);
var tableHeadings = [];

// extract the name of the properties that the object has.
for (var property in parsedResult[0])
{
if (parsedResult[0].hasOwnProperty(property))
tableHeadings.push(property);
}

// make the table
var tbl = newEl('table');
var tbody = newEl('tbody');
tbl.appendChild(tbody);

// make the first row, with headings in it
var tr = newEl('tr');
var i, n = tableHeadings.length;
for (i=0; i<n; i++)
{
var th = newEl('th');
th.innerText = tableHeadings[i];
tr.appendChild(th);
}
tbody.appendChild(tr);

// now for the 'fun' part - the body itself.
var curRowIndex, curColIndex, nRows = parsedResult[0][tableHeadings[0]].length, nCols = tableHeadings.length;
for (curRowIndex=0; curRowIndex<nRows; curRowIndex++)
{
var tr = newEl('tr');
for (curColIndex=0; curColIndex<nCols; curColIndex++)
{
var td = newEl('td');
td.innerText = parsedResult[0][ tableHeadings[curColIndex] ][curRowIndex];
tr.appendChild(td);
}
tbody.appendChild(tr);
}

return tbl;
}
</script>
<style>
</style>
</head>
<body>
</body>
</html>

关于javascript - 如何在表格中显示json字符串的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31531790/

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