Wie kann eine Json Datei in ein Java Objekt eingelesen werden? Es kommt doch hin und wieder mal vor, das man eine Json Datei hat, die man einlesen will. Man könnte es generisch einlesen. Oder man erzeugt sich die nötigen Java BEs bzw. Pojos und liest die Datei dann ein. Hier mal ein Beispiel mit gson zum einlesen in die zuvor erzeugten Java Klassen.
Es muss in der pom.xml diese Abhängigkeit ergänzt werden:
1 2 3 4 5 6 |
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.7</version> <scope>compile</scope> </dependency> |
Z.B. diese aircraft.json Datei. Die Felder werden hier beschrieben. Die Datei wird vom Dump1090 Server alle paar Sekunden aktualisert.
1 2 3 4 5 6 7 8 9 10 11 12 |
{ "now" : 1477853643.7, "messages" : 45918404, "aircraft" : [ {"hex":"471f87","altitude":38025,"vert_rate":-64,"track":287,"speed":393,"mlat":[],"tisb":[],"messages":5,"seen":0.1,"rssi":-35.3}, {"hex":"48520c","squawk":"6441","altitude":38000,"mlat":[],"tisb":[],"messages":26,"seen":7.2,"rssi":-35.0}, {"hex":"3c644a","squawk":"3537","flight":"DLH2TR ","lat":52.605377,"lon":10.574875,"nucp":7,"seen_pos":13.9,"altitude":38025,"vert_rate":0,"track":191,"speed":488,"category":"A0","mlat":[],"tisb":[],"messages":306,"seen":3.8,"rssi":-32.4}, {"hex":"4ca27a","mlat":[],"tisb":[],"messages":1654,"seen":121.9,"rssi":-35.3}, {"hex":"896184","mlat":[],"tisb":[],"messages":4657,"seen":295.6,"rssi":-35.7}, {"hex":"4ca2fe","altitude":37000,"mlat":[],"tisb":[],"messages":7085,"seen":3.6,"rssi":-35.6} ] } |
Wir schauen uns einfach die Struktur an, und erzeugen Java Klasse mit genau den Feldnamen, das ist wichtig da die Felder dann automatisch per reflection eingelesen werden.
Wer erzeugen also eine Klasse Flugzeug, mit den String instanz Variablen hex, altitude, vert_rate usw. Dann generieren wir die Getter und Setter mit Eclipse. Auch eine toString Methode können wir generieren.
Das sieht dann z.B. so aus:
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
package de.wenzlaff.dump1090.be; import java.util.List; /** * Flugzeug Klasse. Pojo gem. * * https://github.com/mutability/dump1090/blob/master/README-json.md * * <pre> aircraft.json This file contains dump1090's list of recently seen aircraft. The keys are: now: the time this file was generated, in seconds since Jan 1 1970 00:00:00 GMT (the Unix epoch). messages: the total number of Mode S messages processed since dump1090 started. aircraft: an array of JSON objects, one per known aircraft. Each aircraft has the following keys. Keys will be omitted if data is not available. hex: the 24-bit ICAO identifier of the aircraft, as 6 hex digits. The identifier may start with '~', this means that the address is a non-ICAO address (e.g. from TIS-B). squawk: the 4-digit squawk (octal representation) flight: the flight name / callsign lat, lon: the aircraft position in decimal degrees nucp: the NUCp (navigational uncertainty category) reported for the position seen_pos: how long ago (in seconds before "now") the position was last updated altitude: the aircraft altitude in feet, or "ground" if it is reporting it is on the ground vert_rate: vertical rate in feet/minute track: true track over ground in degrees (0-359) speed: reported speed in kt. This is usually speed over ground, but might be IAS - you can't tell the difference here, sorry! messages: total number of Mode S messages received from this aircraft seen: how long ago (in seconds before "now") a message was last received from this aircraft rssi: recent average RSSI (signal power), in dbFS; this will always be negative. Beispiel eines Eintrags: "hex":"4596b2","squawk":"1742","lat":51.993682,"lon":11.000218, "nucp":0,"seen_pos":6.1,"altitude":30000,"vert_rate":0,"track":346, "speed":246,"mlat":["lat","lon","track","speed","vert_rate"],"tisb":[],"messages":244,"seen":0.7,"rssi":-32.7}, * * </pre> * * * @author Thomas Wenzlaff * */ public class Flugzeug { /** Der Name der Variable muss so sein wie in der JSON Datei. */ /** * hex: the 24-bit ICAO identifier of the aircraft, as 6 hex digits. The * identifier may start with '~', this means that the address is a non-ICAO * address (e.g. from TIS-B). */ private String hex; /** squawk: the 4-digit squawk (octal representation) */ private String squawk; /** lat: the aircraft position in decimal degrees */ private String lat; /** lon: the aircraft position in decimal degrees */ private String lon; /** * altitude: the aircraft altitude in feet, or "ground" if it is reporting * it is on the ground */ private String altitude; /** * speed: reported speed in kt. This is usually speed over ground, but might * be IAS - you can't tell the difference here, sorry! */ private String speed; /** flight: the flight name / callsign */ private String flight; /** * nucp: the NUCp (navigational uncertainty category) reported for the * position */ private String nucp; /** * seen_pos: how long ago (in seconds before "now") the position was last * updated */ private String seen_pos; /** vert_rate: vertical rate in feet/minute */ private String vert_rate; /** track: true track over ground in degrees (0-359) */ private String track; /** messages: total number of Mode S messages received from this aircraft */ private String messages; /** * seen: how long ago (in seconds before "now") a message was last received * from this aircraft */ private String seen; /** * rssi: recent average RSSI (signal power), in dbFS; this will always be * negative. */ private String rssi; private List<String> mlat; private List<String> tisb; public Flugzeug() { super(); } public String getHex() { return hex; } public void setHex(String hex) { this.hex = hex; } public String getSquawk() { return squawk; } public void setSquawk(String squawk) { this.squawk = squawk; } public String getLat() { return lat; } public void setLat(String lat) { this.lat = lat; } public String getLon() { return lon; } public void setLon(String lon) { this.lon = lon; } public String getAltitude() { return altitude; } public void setAltitude(String altitude) { this.altitude = altitude; } public String getSpeed() { return speed; } public void setSpeed(String speed) { this.speed = speed; } /** * @return das flight */ public String getFlight() { return flight; } /** * @param flight * das flight wird gesetzt */ public void setFlight(String flight) { this.flight = flight; } /** * @return das nucp */ public String getNucp() { return nucp; } /** * @param nucp * das nucp wird gesetzt */ public void setNucp(String nucp) { this.nucp = nucp; } /** * @return das seen_pos */ public String getSeen_pos() { return seen_pos; } /** * @param seen_pos * das seen_pos wird gesetzt */ public void setSeen_pos(String seen_pos) { this.seen_pos = seen_pos; } /** * @return das vert_rate */ public String getVert_rate() { return vert_rate; } /** * @param vert_rate * das vert_rate wird gesetzt */ public void setVert_rate(String vert_rate) { this.vert_rate = vert_rate; } /** * @return das track */ public String getTrack() { return track; } /** * @param track * das track wird gesetzt */ public void setTrack(String track) { this.track = track; } /** * @return das mlat */ public List<String> getMlat() { return mlat; } /** * @param mlat * das mlat wird gesetzt */ public void setMlat(List<String> mlat) { this.mlat = mlat; } /** * @return das tisb */ public List<String> getTisb() { return tisb; } /** * @param tisb * das tisb wird gesetzt */ public void setTisb(List<String> tisb) { this.tisb = tisb; } /** * @return das messages */ public String getMessages() { return messages; } /** * @param messages * das messages wird gesetzt */ public void setMessages(String messages) { this.messages = messages; } /** * @return das seen */ public String getSeen() { return seen; } /** * @param seen * das seen wird gesetzt */ public void setSeen(String seen) { this.seen = seen; } /** * @return das rssi */ public String getRssi() { return rssi; } /** * @param rssi * das rssi wird gesetzt */ public void setRssi(String rssi) { this.rssi = rssi; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Flugzeug ["); if (hex != null) { builder.append("hex="); builder.append(hex); builder.append(", "); } if (squawk != null) { builder.append("squawk="); builder.append(squawk); builder.append(", "); } if (lat != null) { builder.append("lat="); builder.append(lat); builder.append(", "); } if (lon != null) { builder.append("lon="); builder.append(lon); builder.append(", "); } if (altitude != null) { builder.append("altitude="); builder.append(altitude); builder.append(", "); } if (speed != null) { builder.append("speed="); builder.append(speed); builder.append(", "); } if (flight != null) { builder.append("flight="); builder.append(flight); builder.append(", "); } if (nucp != null) { builder.append("nucp="); builder.append(nucp); builder.append(", "); } if (seen_pos != null) { builder.append("seen_pos="); builder.append(seen_pos); builder.append(", "); } if (vert_rate != null) { builder.append("vert_rate="); builder.append(vert_rate); builder.append(", "); } if (track != null) { builder.append("track="); builder.append(track); builder.append(", "); } if (messages != null) { builder.append("messages="); builder.append(messages); builder.append(", "); } if (seen != null) { builder.append("seen="); builder.append(seen); builder.append(", "); } if (rssi != null) { builder.append("rssi="); builder.append(rssi); builder.append(", "); } if (mlat != null && !mlat.isEmpty()) { builder.append("mlat="); builder.append(mlat); builder.append(", "); } if (tisb != null && !tisb.isEmpty()) { builder.append("tisb="); builder.append(tisb); } builder.append("]"); builder.append("\n"); return builder.toString(); } } |
Da wir mehrere Flugzeuge haben könnten wir noch eine Klasse Flugzeuge wie oben erzeugen:
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 |
package de.wenzlaff.dump1090.be; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import de.wenzlaff.dump1090.action.TimerAktion; /** * Flugzeuge. * * @author Thomas Wenzlaff * */ public class Flugzeuge { /** Testmodus wenn auf true, bei false ist Produktion. */ private boolean TEST = true; /** Der Name der Variable muss so sein wie in der JSON Datei. */ private String now; private String messages; private List<Flugzeug> aircraft; private static final Logger LOG = LoggerFactory.getLogger(TimerAktion.class); public Flugzeuge() { super(); if (TEST) { LOG.info("Testmodus ist angeschaltet."); } aircraft = new ArrayList<Flugzeug>(); } /** * Liefert alle Flugzeuge mit Luftnotfall. * * Mit den Codes 7500, 7600 und 7700 werden Informationen über die Art einer * Luftnotlage übermittelt. * * Gebräuchliche Merksprüche sind angefügt: * * <pre> 7500 – Flugzeugentführung (hijacking; seven-five - man with a knife) 7600 – Funkausfall (radio failure; seven-six - hear nix / radio nix / need a radio fix) 7700 – Luftnotfall (emergency; seven-seven - going to heaven / falling from heaven / pray to heaven) * </pre> * * @return */ public List<Flugzeug> getNotfall() { List<Flugzeug> notfallFlugzeuge = new ArrayList<Flugzeug>(); for (Flugzeug flugzeug : aircraft) { if (flugzeug.getSquawk() != null) { if (flugzeug.getSquawk().equals("7500") || flugzeug.getSquawk().equals("7600") || flugzeug.getSquawk().equals("7700")) { notfallFlugzeuge.add(flugzeug); } } } if (TEST) { // testweise ein Flugzeug ergänzen notfallFlugzeuge.add(aircraft.get(0)); } return notfallFlugzeuge; } public void addFlugzeug(Flugzeug f) { aircraft.add(f); } /** * @return das flugzeuge */ public List<Flugzeug> getFlugzeuge() { return aircraft; } public int getAnzahlFlugzeuge() { return aircraft.size(); } /** * @return das now */ public String getNow() { return now; } /** * @param now * das now wird gesetzt */ public void setNow(String now) { this.now = now; } /** * @return das messages */ public String getMessages() { return messages; } /** * @param messages * das messages wird gesetzt */ public void setMessages(String messages) { this.messages = messages; } /** * @param flugzeuge * das flugzeuge wird gesetzt */ public void setFlugzeuge(List<Flugzeug> flugzeuge) { aircraft = flugzeuge; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Flugzeuge ["); if (now != null) { builder.append("now="); builder.append(now); builder.append(", "); } if (messages != null) { builder.append("messages="); builder.append(messages); builder.append(", "); builder.append("\n"); } if (aircraft != null) { builder.append("aircraft="); builder.append(aircraft); } builder.append("]"); builder.append("\n"); return builder.toString(); } } |