gpt4 book ai didi

javascript - 重新保护 JSON 响应的访问值

转载 作者:行者123 更新时间:2023-11-29 18:02:35 25 4
gpt4 key购买 nike

我正在尝试从我的 json 响应中访问所需的值,以便加载到下拉框中。但是我得到 undefined 作为下拉项。

这是我的 json 响应:

{"currency":[{"code":"USD","dateModified":"2015-10-30T20:43:58","decimalPlace":"2","id":1,"status":true,"symbolLeft":"$","symbolRight":"","tiendayaOrderses":[],"title":"US Dollar","value":1.00000000},{"code":"EUR","dateModified":"2015-10-30T20:43:58","decimalPlace":"2","id":2,"status":true,"symbolLeft":"","symbolRight":"€","tiendayaOrderses":[],"title":"Euro","value":0.76775432},{"code":"GBP","dateModified":"2015-10-30T20:43:58","decimalPlace":"2","id":3,"status":true,"symbolLeft":"£","symbolRight":"","tiendayaOrderses":[],"title":"Pound Sterling","value":0.64524455}]}

以下是我获取回复的方式:

$(document).ready(function() {
$.ajax({
url: "currencies",
dataType: 'json',
error: function() {
alert('error');
},
success: function(data) {
//var jsonData = $.parseJSON(data);
$.each(data.currency, function(index) {
$.each(data.currency[index], function(key, value) {
//curncyDrop is the drop-down box
$('#curncyDrop').append('<li><a>' + value.title +
'</a></li>');


});

});
}

});
});

这会将所有数据加载到下拉列表中,而不是加载所需的 (title) 值:

$('#curncyDrop').append('<li><a>' + value + '</a></li>');

提前致谢。

最佳答案

var data = {
"currency": [{
"code": "USD",
"dateModified": "2015-10-30T20:43:58",
"decimalPlace": "2",
"id": 1,
"status": true,
"symbolLeft": "$",
"symbolRight": "",
"tiendayaOrderses": [],
"title": "US Dollar",
"value": 1.00000000
}, {
"code": "EUR",
"dateModified": "2015-10-30T20:43:58",
"decimalPlace": "2",
"id": 2,
"status": true,
"symbolLeft": "",
"symbolRight": "€",
"tiendayaOrderses": [],
"title": "Euro",
"value": 0.76775432
}, {
"code": "GBP",
"dateModified": "2015-10-30T20:43:58",
"decimalPlace": "2",
"id": 3,
"status": true,
"symbolLeft": "£",
"symbolRight": "",
"tiendayaOrderses": [],
"title": "Pound Sterling",
"value": 0.64524455
}]
}



$.each(data, function(index, value){
$.each(value, function(indx, val){
$('#curncyDrop').append('<li><a>' + val.title + '</a></li>');
$('#curncyDrop').append('<li><a>' + val.value + '</a></li>');
})
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id='curncyDrop'></ul>

这样试试

关于javascript - 重新保护 JSON 响应的访问值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33839859/

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