到目前为止,我列出了每个航班的航空公司及其价格,现在我想从 json 列表中获取有关这些航班的所有信息,这里有以下功能:
第一个功能工作正常:
function show_airlines() {
$.getJSON("http://api.anywayanyday.com/api/NewRequest/?Route=2406MOWLON&AD=1&CN=0&CS=E&Partner=testapic&_Serialize=JSON&callback=?", function(data) {
var id=data.Id;
$('#search_id').attr('rel',id);
$.getJSON("http://api.anywayanyday.com/api/Fares/?R="+id+"&V=Matrix&VB=true&L=ru&_Serialize=JSON&callback=?", function(data) {
var pricesForEachAirline = [];
$.each(data.Airlines, function() {
var item = { name : this.Name, prices : [], code: this.Code, airid: []};
for(var y in this.FaresFull)
{
item.prices.push(this.FaresFull[y].TotalAmount);
item.airid.push(this.FaresFull[y].FareId);
}
pricesForEachAirline.push(item);
});
$.each(pricesForEachAirline , function(){
$('#airlines').append('<a href="javascript://" onclick="show_flights('+ this.airid +');">'+ this.name +'</a>').append('</br>');
$('#prices').append(this.prices.join(',')).append('</br>');
});
});
});
}
第二个不能正常工作...
function show_flights() {
var id=$('#search_id').attr('rel');
var list = Array.prototype.slice.call(arguments, 0);
$.getJSON("http://api.anywayanyday.com/api/Fares/?R="+id+"&V=Matrix&VB=true&L=ru&_Serialize=JSON&callback=?", function(data) {
var dataForEachFlight = [];
$.each(data.Airlines, function() {
for(var x in this.FaresFull)
{
if(this.FaresFull[x].FareId in list)
{
var flight = { from : [], to : []}
for(var y in this.FaresFull.Directions.Segments.Trips)
{
flight.from.push(this.FaresFull.Directions.Segments.Trips.Departure[y].AirportCode);
flight.to.push(this.FaresFull.Directions.Segments.Trips.Arrival[y].AirportCode);
}
dataForEachFlight.push(flight);
}
}
});
$.each(dataForEachFlight , function(){
$('#flights').append(this.from.join(',')).append('</br>');
});
});
}
因此,当我单击航空公司链接时,我只想获取有关其航班的信息,但它给出的错误如下:this.FaresFull.Directions 未定义,并且作为我想从中获取所有信息的 json 示例:http://vteem.net/json.json(这只是例子,你可以从链接函数中得到原始的)
谢谢大家的帮助,我真的很感激!
功能 2 更新
function show_flights() {
var id=$('#search_id').attr('rel');
var list = Array.prototype.slice.call(arguments, 0);
$.getJSON('http://api.anywayanyday.com/api/Fares/?R='+id+'&V=Matrix&VB=true&L=ru&_Serialize=JSON&callback=?', function(data) {
var dataForEachFlight = [];
$.each(data.Airlines, function() {
for(var a in this.FaresFull)
{
for(var b in this.FaresFull[a].FareId)
{
if(this.FaresFull[a].FareId[b] in list)
{
var flight = { from : [], to : []};
for(var c in this.FaresFull[a].Directions[0].Segments[0].Trips)
{
flight.from.push(this.FaresFull[a].Directions[0].Segments[0].Trips[c].Departure.AirportCode);
flight.to.push(this.FaresFull[a].Directions[0].Segments[0].Trips[c].Arrival.AirportCode);
}
dataForEachFlight.push(flight);
}
}
}
});
$.each(dataForEachFlight , function(){
$('#flights').append(this.from.join(',')).append('</br>');
});
});
}
试试#12
现在它获取了数据,但是它重复了太多:) for() 子句中的一些错误。