无法从json列表中获取数据 [英] cant get data from json list

查看:83
本文介绍了无法从json列表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经列出了每个航班的航空公司及其价格列表,现在我想从json列表中获取有关这些航班的所有信息,无论如何,这里是函数:

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:

第一个功能运行正常:

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

因此,当我单击航空公司链接时,我只想获取有关其航班的信息,但它给出了如下错误: this.FaresFull.Directions未定义,并作为json的示例我想从中获取所有信息: http://vteem.net/json.json (此仅仅是示例,您可以从链接函数中获得原始信息)

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!

功能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()子句中有些错误.

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

推荐答案

好,我已经修改了最后一个解决方案.但是结果不是最终的,它只是向您显示解决问题的方式(它会打印出每个航空公司的所有价格以及所有关联的出发和到达机场代码).您可以找到演示此处. 代码:

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

});});​

希望,对您有用!)

这篇关于无法从json列表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆