1

到目前为止,我列出了每个航班的航空公司及其价格,现在我想从 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() 子句中的一些错误。

4

2 回答 2

2

好吧,我已经修改了我们最后的解决方案。但结果不是最终结果,它只是向您展示了解决问题的方法(它打印出所有价格以及每个航空公司的所有连接出发和到达机场代码)。你可以在这里找到演示。代码:

$.getJSON("http://api.anywayanyday.com/api/NewRequest/?Route=2406MOWLON&AD=1&CN=0&CS=E&Partner=testapic&_Serialize=JSON&callback=?", function(data) {
var code=data.Id;
$.getJSON("http://api.anywayanyday.com/api/Fares/?R="+code+"&V=Matrix&VB=true&L=ru&_Serialize=JSON&callback=?", function(data) {
    var pricesForEachAirline = [];
    $.each(data.Airlines, function() { 

        var item = { name : this.Name, infos: []};            

        $.each(this.FaresFull, function()
        {
            var info = {departures:[], arrivals:[]}; 
            info.price=this.TotalAmount;
            $.each(this.Directions, function(){
                $.each(this.Segments, function(){
            $.each(this.Trips, function()
            {
                info.departures.push(this.Departure.AirportCode);
                info.arrivals.push(this.Arrival.AirportCode);
            });
                   });
        });
            item.infos.push(info);
        });
           pricesForEachAirline.push(item);
    });
    $.each(pricesForEachAirline , function(){
        $('#data').append(this.name+":").append('</br>');
        $.each(this.infos, function(){
            $('#data').append(this.price+"&nbsp; DEPARTURES:").append(this.departures.join(',')).append("&nbsp; ARRIVALS:").append(this.arrivals.join(',')).append("</br></br>");
        });
    });

});});​

希望对你有用!)

于 2012-09-28T14:43:45.493 回答
1

不确定,也没有自己测试过,但可能是您忘记了FaresFull元素的索引x吗?

我的意思是,您的代码不应该总是调用this.FaresFull[ x ]并变成这样:

for(var x in this.FaresFull)
{    
    if(this.FaresFull[x].FareId in list) 
    { 
        var flight = { from : [], to : []};
        // for testing only first Directions/Segments array element taken 
        for(var y in this.FaresFull[x].Directions[0].Segments[0].Trips) 
        {                
            flight.from.push(this.FaresFull[x].Directions[0].Segments[0].Trips[y].Departure.AirportCode);
            flight.to.push(this.FaresFull[x].Directions[0].Segments[0].Trips[y].Arrival.AirportCode);
        }
        dataForEachFlight.push(flight);
    }
}

编辑:更正的代码,另见http://jsfiddle.net/LZ8wN/2/

edit2: 您可能想尝试这种替代方法来遍历数组。请注意,我在您的航班声明和分配的末尾添加了一个分号。

$.each(data.Airlines, function() { 
    var flight = { from : [], to : []};
    $.each(this.FaresFull,function(w) {
        if(this.FareId in list) 
        {
            $.each(this.Directions,function(x) {
                $.each(this.Segments,function(y) {
                    $.each(this.Trips,function(z) {
                        flight.from.push(this.Departure.AirportCode);
                        flight.to.push(this.Arrival.AirportCode);
                    });
                    dataForEachFlight.push(flight);
                });
            });
        }
    });
});
于 2012-09-27T11:14:36.857 回答