-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSON_pohja_junat.java
179 lines (137 loc) · 4.98 KB
/
JSON_pohja_junat.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package fi.academy.json.esimerkki;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/*
Vaatii Jackson kirjaston:
File | Project Structure
Libraries >> Add >> Maven
Etsi "jackson-databind", valitse esimerkiksi versio 2.0.5
Asentuu Jacksonin databind, sekä core ja annotations
*/
public class JSON_pohja_junat {
public static void main(String[] args) {
lueJunanJSONData();
}
private static void lueJunanJSONData() {
String baseurl = "https://rata.digitraffic.fi/api/v1";
try {
URL url = new URL(URI.create(String.format("%s/live-trains/station/HKI/LH", baseurl)).toASCIIString());
ObjectMapper mapper = new ObjectMapper();
CollectionType tarkempiListanTyyppi = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Juna.class);
List<Juna> junat = mapper.readValue(url, tarkempiListanTyyppi); // pelkkä List.class ei riitä tyypiksi
System.out.println(junat.get(0).getTrainNumber());
// Seuraavaa varten on toteutettava TimeTableRow luokka:
//System.out.println(junat.get(0).getTimeTableRows().get(0).getScheduledTime());
System.out.println("\n\n");
System.out.println(junat.get(0));
} catch (Exception ex) {
System.out.println(ex);
}
}
}
class Juna {
boolean cancelled;
String commuterLineID;
//LocalDate departureDate; // Jackson ei oikein pidä Java 8 päivistä oletuksena
Date departureDate;
String operatorShortCode;
int operatorUICCode;
boolean runningCurrently;
List<TimeTableRow> timeTableRows;
Date timetableAcceptanceDate;
String timetableType;
String trainCategory;
int trainNumber;
String trainType;
long version;
@Override
public String toString() {
return "Juna{" + "cancelled=" + cancelled + ", commuterLineID='" + commuterLineID + '\'' + ", departureDate=" + departureDate + ", operatorShortCode='" + operatorShortCode + '\'' + ", operatorUICCode=" + operatorUICCode + ", runningCurrently=" + runningCurrently + ", timeTableRows=" + timeTableRows + ", timetableAcceptanceDate=" + timetableAcceptanceDate + ", timetableType='" + timetableType + '\'' + ", trainCategory='" + trainCategory + '\'' + ", trainNumber=" + trainNumber + ", trainType='" + trainType + '\'' + ", version=" + version + '}';
}
public boolean isCancelled() {
return cancelled;
}
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
public String getCommuterLineID() {
return commuterLineID;
}
public void setCommuterLineID(String commuterLineID) {
this.commuterLineID = commuterLineID;
}
public Date getDepartureDate() {
return departureDate;
}
public void setDepartureDate(Date departureDate) {
this.departureDate = departureDate;
}
public String getOperatorShortCode() {
return operatorShortCode;
}
public void setOperatorShortCode(String operatorShortCode) {
this.operatorShortCode = operatorShortCode;
}
public int getOperatorUICCode() {
return operatorUICCode;
}
public void setOperatorUICCode(int operatorUICCode) {
this.operatorUICCode = operatorUICCode;
}
public boolean isRunningCurrently() {
return runningCurrently;
}
public void setRunningCurrently(boolean runningCurrently) {
this.runningCurrently = runningCurrently;
}
public List<TimeTableRow> getTimeTableRows() {
return timeTableRows;
}
public void setTimeTableRows(List<TimeTableRow> timeTableRows) {
this.timeTableRows = timeTableRows;
}
public Date getTimetableAcceptanceDate() {
return timetableAcceptanceDate;
}
public void setTimetableAcceptanceDate(Date timetableAcceptanceDate) {
this.timetableAcceptanceDate = timetableAcceptanceDate;
}
public String getTimetableType() {
return timetableType;
}
public void setTimetableType(String timetableType) {
this.timetableType = timetableType;
}
public String getTrainCategory() {
return trainCategory;
}
public void setTrainCategory(String trainCategory) {
this.trainCategory = trainCategory;
}
public int getTrainNumber() {
return trainNumber;
}
public void setTrainNumber(int trainNumber) {
this.trainNumber = trainNumber;
}
public String getTrainType() {
return trainType;
}
public void setTrainType(String trainType) {
this.trainType = trainType;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
class TimeTableRow {
}