到目前为止,我列出了每个航班的航空公司及其价格,现在我想从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()子句中的一些错误。
答案 0 :(得分:2)
好吧,我修改了我们的最后一次solution。但结果不是最终的,它只是告诉你应对你问题的方式(它打印出所有航空公司的所有价格和所有相关的出发和到达机场代码)。你可以找到demo HERE。 代码:
$.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+" DEPARTURES:").append(this.departures.join(',')).append(" ARRIVALS:").append(this.arrivals.join(',')).append("</br></br>");
});
});
});});
希望,这对你有用!)
答案 1 :(得分: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);
});
});
}
});
});