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>');
            }); 
        });
    });
}

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 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 is undefinedであり、すべての情報を取得する json の例: http://vteem.net/json.json (これは単なる例です。元の u はリンク関数から取得できます)

助けてくれてありがとう、本当に感謝しています!

機能 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 に答える