我有一个带有多个嵌套数组和对象的对象的 json 文件,我很难找到一种方法来遍历它并获取所有数据,因为属性嵌套在对象内部的各种“位置”中。我试图简化对象并希望它不会太大:
{
"trips": [{
"trains": {
"originTrains": {
"originTrain": [{
//always 2 x trainSegment - 1 for train to location and return train
"trainSegment": [{
//unknown numbers of objects inside trainSegment array
"Departure": {
"Location": "xxx"
},
"Arrival": {
"Location": "yyy"
},
"duration": 195,
}, {
"Departure": {
"Location": "yyy"
},
"Arrival": {
"Location": "zzz"
},
"duration": 240,
}],
"duration": 485
}, {
"trainSegment": [{
"Departure": {
"Location": "zzz"
},
"Arrival": {
"Location": "mmm"
},
"duration": 160,
}, {
"Departure": {
"Location": "mmm"
},
"Arrival": {
"Location": "xxx"
},
"duration": 325,
}],
"duration": 745
}]
},
"Direction": "Return"
},
"PriceInfo": {
"Breakdown": {
"FareBreakdown": {
"PassengerQuantity": {
"Quantity": 1,
"Code": "ABC"
},
"Endorsements": {
"NotRefundable": true
}
}
},
"FareInfos": {
"FareInfo": [{
"Extensions": {
"SeatsRemaining": {
"BelowMinimum": false,
"Number": 4
}
}
}]
},
"Fare": {
"TotalFare": {
"CurrencyCode": "USD",
"DecimalPlaces": 2,
"Amount": 341.61
},
"Taxes": {
"Tax": [{
"CurrencyCode": "USD",
"DecimalPlaces": 2,
"TaxCode": "TOTALTAX",
"Amount": 66.25
}]
},
"BaseFare": {
"CurrencyCode": "USD",
"DecimalPlaces": 2,
"Amount": 275.36
}
}
},
"TicketingInfo": {
"TicketType": "eTicket"
}
}, {
"trains": {
"originTrains": {
//all over again
}
最终我想显示所有信息(作为所有火车的起点、目的地、时间、持续时间。到目前为止我尝试过的任何事情都没有真正起作用。我没有提供我的代码,因为我所有的循环都没有意义并且没有'不起作用,但当然可以提供。非常感谢任何帮助。谢谢。