cant get data from json list

2019-07-27 07:33发布

问题:

so far i made a list of airlines and their prices for each flight, and now i want to get all the information about these flights from json list, any way here are the functions:

first function works fine:

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

The Second one doesnt work properly...

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

so when i click on the airline link i want to get the information about it's flights only, but it gives out the mistake like: this.FaresFull.Directions is undefined and as an example of json from which i want to get all the information: http://vteem.net/json.json (this is just example, the original u can get from link function)

thank you all for the help, i really appreciate it!

FUNCTION 2 UPDATE

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

}

try #12

Now it gets the data, but it repeats too much :) some mistake in for() clauses.

回答1:

Well, I've modified our last solution . But result is not final, it just shows you the way to cope with you issue(it prints out all pricess and all connected departures and arrivals airport codes for each of airline). You can find demo HERE. Code:

$.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>");
        });
    });

});});​

Hope, it will be usefull for you!)



回答2:

Not sure, and didn't test it out myself but could it be that you just forgot the index x of your FaresFull element?

I mean, shouldn't your code always call this.FaresFull[x] and become this:

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

edit: corrected code, see also http://jsfiddle.net/LZ8wN/2/

edit2: You might want to try this alternative way to traverse the arrays. Note that I added a semicolon to the end of your flight declaration and asssignment.

$.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);
                });
            });
        }
    });
});