我刚刚发现了这个很棒的网站,我允许自己用以下问题来打扰您:
如何从内部(嵌套)JSON 对象访问字段,这里从 JSON 对象“flights”中命名为“flightLegs”?
为了通过 GWT 应用程序从外部 API(此处为 flightstats)访问 JSON 对象,我使用以下方法:
这是 JSON 对象(我通过 api 正确接收到它,我也可以在主页上查看它):
"flights": [
{
"departureAirportFsCode": "ZRH",
"arrivalAirportFsCode": "NRT",
"departureDateFrom": "2013-01-28",
"departureDateTo": "2013-03-30",
"departureDaysOfWeek": [
1,
2,
3,
4,
5,
6,
7
],
"arrivalDateAdjustment": 1,
"departureTime": "13:00:00.000",
"arrivalTime": "08:55:00.000",
"distanceMiles": 5969,
"flightDurationMinutes": 715,
"layoverDurationMinutes": 0,
"flightType": "NON_STOP",
"serviceType": "PASSENGER_ONLY",
"online": true,
"flightLegs": [
{
"departureAirportFsCode": "MUC",
"arrivalAirportFsCode": "NRT",
"departureTime": "13:00:00.000",
"arrivalTime": "08:55:00.000",
"departureDateAdjustment": 0,
"arrivalDateAdjustment": 1,
"arrivalTerminal": "1",
"carrierFsCode": "LX",
"flightNumber": "160",
"codeshare": false,
"equipmentCodes": [
"343"
],
"distanceMiles": 5969,
"flightDurationMinutes": 715,
"layoverDurationMinutes": 0
}
]
例如,我可以使用以下代码为外部 JSON 对象“航班”的所有字段创建叠加层:
public final native String getDepartureFromAirport() /*-{
return this.departureAirportFsCode;
}-*/;
这也很完美。
但我还需要访问 Flightnumber,即来自“flightLegs”JSON 对象的字段“flightnumber”。我试过:
public final native String getDepartureFromAirport() /*-{
return this.flightLegs.flightNumber;
}-*/;
但这不起作用,它只会给我留下一个空对象或空对象(根据错误消息)。所以我很不知道如何访问内部 JSON 对象。
老实说,任何帮助将不胜感激!非常感谢!