gpt4 book ai didi

javascript - 使用ajax在HTML表中显示嵌套的json

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

我有以下 json 数据作为示例。但是,我无法将其显示在 html 表中。但是,它只返回 3 列,其中显示“未定义”。如何正确访问我的 json 数据?

{
"count": 3,
"entries": [
{
"affiliation": "Establishment1",
"city": "City1",
"code": "Code1",
"contact": "Contact1",
"firstname": "firstname1",
"lastname": "lastname1",
"middleinitial": "middleinitial1"
},
{
"affiliation": "Establishment2",
"city": "City2",
"code": "Code2",
"contact": "Contact2",
"firstname": "firstname2",
"lastname": "lastname2",
"middleinitial": "middleinitial2"
},
{
"affiliation": "Establishment3",
"city": "City3",
"code": "Code3",
"contact": "Contact3",
"firstname": "firstname3",
"lastname": "lastname3",
"middleinitial": "middleinitial3"
},
],
"status": "ok"
}

我尝试这样做以将其显示在 html 表格中:

$(document).ready(function () {
$.getJSON('http://127.0.0.1:5000/counselorss', function (data) {
var mdata = '';
$.each(data, function (key, value) {
mdata += '<tr>';
mdata += '<td>'+value.lastname + '</td>';
mdata += '<td>'+value.firstname + '</td>';
mdata += '<td>'+value.middleinitial + '</td>';
mdata += '<td>'+value.contact + '</td>';
mdata += '<td>'+value.affiliation + '</td>';
mdata += '<td>'+value.city + '</td>';
mdata += '<td>'+value.code + '</td>';
mdata += '</tr>';
});
$('#counselorstable').append(counselor_data);
});
});

最佳答案

试试这个:

    $(document).ready(function () {
$.getJSON('http://127.0.0.1:5000/counselorss', function (data) {
var mdata = '';
data = JSON.parse(data);
$.each(data['entries'], function (key, value) {
mdata += '<tr>';
mdata += '<td>'+value.lastname + '</td>';
mdata += '<td>'+value.firstname + '</td>';
mdata += '<td>'+value.middleinitial + '</td>';
mdata += '<td>'+value.contact + '</td>';
mdata += '<td>'+value.affiliation + '</td>';
mdata += '<td>'+value.city + '</td>';
mdata += '<td>'+value.code + '</td>';
mdata += '</tr>';
});
$('#counselorstable').append(counselor_data);
});
});

关于javascript - 使用ajax在HTML表中显示嵌套的json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51042358/

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