gpt4 book ai didi

jquery - 显示 JSON 对象数组内的元素

转载 作者:行者123 更新时间:2023-12-01 01:21:17 24 4
gpt4 key购买 nike

我可以显示 JSON 对象的一个​​元素,但如果它位于数组内,那么我就会陷入困境。

这是我的代码:

   $.ajax({
method: "GET",
url: api_url,
// url: mock_url,
contentType: "application/json",
data: adminPrefData,
success: function (res) {
console.log(res);
// callback(res);
$('#module').text(res.module); // i get this
$('#networkId').text(res.networkId); // and this
$('#adminPrefInfo.type').text(res.adminPrefInfo.type); // nothing happens on page
},
error: function () {
$('#info').html('<p>An error has occured</p>');
},
});

这是我的 HTML:

 <div class="content">
<div class="nav-link-content home">
<h1 class="text-center" id="module"></h1>
<div class="text-center" id="networkId"></div>
<div class="text-center" id="adminPrefInfo.type"></div>
</div>
</div>

这是我得到的 JSON:

{"module":"dnd","networkId":1,"adminPrefInfo":[{"id":1,"key":"teszt","value":"teszt","defValue":"teszt1","type":"checkbox","isActive":true}]}

最佳答案

第一个问题是因为 HTML 元素的 id 属性中有句点。您需要在 jQuery 选择器中使用 \\ 对其进行转义。第二个问题是 adminPrefInfo 是一个数组。因此,您需要通过索引访问它,然后获取 type 属性。试试这个:

$('#adminInfoList\\.type').text(res.adminPrefInfo[0].type);

关于jquery - 显示 JSON 对象数组内的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59178198/

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