gpt4 book ai didi

javascript - 如何格式化 JSON 数据以在不同的 div 中显示结果对象

转载 作者:行者123 更新时间:2023-12-03 02:50:03 25 4
gpt4 key购买 nike

所以我开始使用 Amadeus REST API,并一直使用 AJAX 调用来获取数据,但现在我必须以某种格式表示该数据,并且我不知道如何循环或显示数据

function showdataforflights(data) {
jQuery.each(data, function(index, item) {
console.log(item.itineraries.outbound.flights.departs_at);
console.log('wadasd');
});
};

$("#main_form").on('submit', (function(e) {

e.preventDefault();

$('.loader').show();
$("html, body").scrollTop(0);

$.ajax({
url: "./search/amadeus.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData($('form')[0]), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
processData: false,
contentType: false,
success: function(data) // A function to be called if request succeeds
{
$('.loader').hide();
$('#lala').text(JSON.stringify(data.results));
showdataforflights(data.results);
}
});
}));

这就是我被困住的地方。我不知道如何循环数据以便可以以特定的 HTML 格式显示数据。

JSON:

{
"currency": "USD",
"results": [
{
"itineraries": [
{
"outbound": {
"flights": [
{
"departs_at": "2017-12-30T13:00",
"arrives_at": "2017-12-30T15:10",
"origin": {
"airport": "DEL",
"terminal": "3"
},
"destination": {
"airport": "BOM",
"terminal": "2"
},
"marketing_airline": "AI",
"operating_airline": "AI",
"flight_number": "863",
"aircraft": "744",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "S",
"seats_remaining": "2"
}
}
]
}
}
],
"fare": {
"total_price": "73.39",
"price_per_adult": {
"total_fare": "73.39",
"tax": "8.39"
},
"restrictions": {
"refundable": true,
"change_penalties": true
}
}
},
{
"itineraries": [
{
"outbound": {
"flights": [
{
"departs_at": "2017-12-30T04:00",
"arrives_at": "2017-12-30T06:15",
"origin": {
"airport": "DEL",
"terminal": "3"
},
"destination": {
"airport": "BOM",
"terminal": "2"
},
"marketing_airline": "AI",
"operating_airline": "AI",
"flight_number": "349",
"aircraft": "788",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "U",
"seats_remaining": "9"
}
}
]
}
}
],
"fare": {
"total_price": "106.99",
"price_per_adult": {
"total_fare": "106.99",
"tax": "9.99"
},
"restrictions": {
"refundable": true,
"change_penalties": true
}
}
},
{
"itineraries": [
{
"outbound": {
"flights": [
{
"departs_at": "2017-12-30T08:00",
"arrives_at": "2017-12-30T10:10",
"origin": {
"airport": "DEL",
"terminal": "3"
},
"destination": {
"airport": "BOM",
"terminal": "2"
},
"marketing_airline": "9W",
"operating_airline": "9W",
"flight_number": "336",
"aircraft": "73H",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "H",
"seats_remaining": "1"
}
}
]
}
}
],
"fare": {
"total_price": "116.35",
"price_per_adult": {
"total_fare": "116.35",
"tax": "45.35"
},
"restrictions": {
"refundable": true,
"change_penalties": true
}
}
},
{
"itineraries": [
{
"outbound": {
"flights": [
{
"departs_at": "2017-12-30T06:00",
"arrives_at": "2017-12-30T08:05",
"origin": {
"airport": "DEL",
"terminal": "3"
},
"destination": {
"airport": "BOM",
"terminal": "2"
},
"marketing_airline": "UK",
"operating_airline": "UK",
"flight_number": "975",
"aircraft": "320",
"booking_info": {
"travel_class": "PREMIUM_ECONOMY",
"booking_code": "U",
"seats_remaining": "9"
}
}
]
}
}
],
"fare": {
"total_price": "122.61",
"price_per_adult": {
"total_fare": "122.61",
"tax": "19.61"
},
"restrictions": {
"refundable": true,
"change_penalties": true
}
}
},
{
"itineraries": [
{
"outbound": {
"flights": [
{
"departs_at": "2017-12-30T20:05",
"arrives_at": "2017-12-30T22:05",
"origin": {
"airport": "DEL",
"terminal": "1D"
},
"destination": {
"airport": "BOM",
"terminal": "1"
},
"marketing_airline": "H1",
"operating_airline": "SG",
"flight_number": "9071",
"aircraft": "737",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "L",
"seats_remaining": "2"
}
}
]
}
}
],
"fare": {
"total_price": "185.45",
"price_per_adult": {
"total_fare": "185.45",
"tax": "84.45"
},
"restrictions": {
"refundable": false,
"change_penalties": true
}
}
}
]
}

最佳答案

尝试使用for():

function showdataforflights(data) {
for(var i in data.results){
//Here is the current Item
var currentData = data[i];

//And here you can acccess all of your properties
var itineraries = currentData.itineraries;
var fare = currentData.fare;
}
};

这是一个基本示例,请尝试查看相关的 question

关于javascript - 如何格式化 JSON 数据以在不同的 div 中显示结果对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47913040/

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