Potrzebuję parsować taki content z api:
{"full_count":12266,"version":4,"214e409c":["70C778",17.0480,54.1086,271,0,0,"0000","F-OOSA2","","",1565517192,"SLL","","",1,0,"",0,""]
,"214e409e":["70C77A",17.0332,54.0834,54,0,0,"0000","F-OOSA2","","",1565517191,"SLL","","",1,0,"",0,""]
,"216a0374":["71D423",37.4558,126.4807,180,0,0,"0000","F-RKSI1","","",1565517191,"ICN","","",1,0,"",0,""]
,"216cb337":["896E0C",24.4206,54.6598,99,0,0,"3443","T-OMAA4","","",1565517188,"","","",1,0,"TTS1",0,""]
}
Parser w javie:
package com.hello.nope.app.Utils;
import com.hello.nope.app.Entities.FlightInfo;
import com.hello.nope.app.Entities.Radar;
import java.util.ArrayList;
import java.util.List;
public class RadarParser {
public static Radar parseJsonToRadarObj(String jsonData) {
int allAirPlanes = Integer.parseInt(jsonData.substring(jsonData.indexOf("\"full_count\":")+13, jsonData.indexOf(",\"")));
int version = Integer.parseInt(jsonData.substring(jsonData.indexOf("\"version\"")+10, jsonData.indexOf(",\"", jsonData.indexOf("\"version\""))));
String[] lines = null;
if(jsonData.endsWith("] }"))
{
jsonData = jsonData.substring(jsonData.indexOf(",\"", jsonData.indexOf(",\"") + 1) + 1, jsonData.indexOf("] }")).replace(":[", ",");
lines = jsonData.split("] ,");
}
return buildRadarObj(allAirPlanes, version, lines);
}
private static Radar buildRadarObj(int allAirPlanes, int version, String[] lines) {
List<FlightInfo> flightInfoList = new ArrayList<>();
String[] params;
for(String l : lines) {
params = l.split(",");
for(int i=0;i<params.length; ++i) {
params[i] = params[i].replace("\"\"", "0");
params[i] = params[i].replace("\"", "");
}
flightInfoList.add(new FlightInfo(params[0], params[1], Double.parseDouble(params[2]), Double.parseDouble(params[3]),
Integer.parseInt(params[4]), Integer.parseInt(params[5]), Integer.parseInt(params[6]), Integer.parseInt(params[7]),
params[8], params[9], params[10], Integer.parseInt(params[11]),
params[12], params[13], params[14], Integer.parseInt(params[15]),
Integer.parseInt(params[16]), params[17], Integer.parseInt(params[18]), params[19]));
}
return new Radar(allAirPlanes, version, flightInfoList);
}
}
Radar.java
package com.hello.nope.app.Entities;
import java.util.List;
public class Radar {
private int allAirPlanes;
private int version;
private List<FlightInfo> flightInfoList;
public Radar(int allAirPlanes, int version, List<FlightInfo> flightInfoList) {
this.allAirPlanes = allAirPlanes;
this.version = version;
this.flightInfoList = flightInfoList;
}
public int getAllAirPlanes() {
return allAirPlanes;
}
public void setAllAirPlanes(int allAirPlanes) {
this.allAirPlanes = allAirPlanes;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public List<FlightInfo> getFlightInfoList() {
return flightInfoList;
}
public void setFlightInfoList(List<FlightInfo> flightInfoList) {
this.flightInfoList = flightInfoList;
}
@Override
public String toString() {
return "Radar{" +
"allAirPlanes=" + allAirPlanes +
", version=" + version +
", flightInfoList=" + flightInfoList +
'}';
}
}
FlightInfo.java
package com.hello.nope.app.Entities;
public class FlightInfo {
private String id;
private String icao24bitAddr;
private Double actualLatitude;
private Double actualLongitude;
private Integer trackDirectionAircraft;
private Integer altitude;
private Integer speed;
private Integer someValue1;
private String someValue2;
private String airCraftType;
private String registrationNumber;
private Integer someValue3;
private String countryFrom;
private String countryTo;
private String trackFlight;
private Integer someValue4;
private Integer someValue5;
private String airLine;
private Integer someValue6;
private String airLineShortName;
public FlightInfo(String id, String icao24bitAddr, Double actualLatitude, Double actualLongitude, Integer trackDirectionAircraft, Integer altitude, Integer speed, Integer someValue1, String someValue2, String airCraftType, String registrationNumber, Integer someValue3, String countryFrom, String countryTo, String trackFlight, Integer someValue4, Integer someValue5, String airLine, Integer someValue6, String airLineShortName) {
this.id = id;
this.icao24bitAddr = icao24bitAddr;
this.actualLatitude = actualLatitude;
this.actualLongitude = actualLongitude;
this.trackDirectionAircraft = trackDirectionAircraft;
this.altitude = altitude;
this.speed = speed;
this.someValue1 = someValue1;
this.someValue2 = someValue2;
this.airCraftType = airCraftType;
this.registrationNumber = registrationNumber;
this.someValue3 = someValue3;
this.countryFrom = countryFrom;
this.countryTo = countryTo;
this.trackFlight = trackFlight;
this.someValue4 = someValue4;
this.someValue5 = someValue5;
this.airLine = airLine;
this.someValue6 = someValue6;
this.airLineShortName = airLineShortName;
}
public String getIcao24bitAddr() {
return icao24bitAddr;
}
public void setIcao24bitAddr(String icao24bitAddr) {
this.icao24bitAddr = icao24bitAddr;
}
public Double getActualLatitude() {
return actualLatitude;
}
public void setActualLatitude(Double actualLatitude) {
this.actualLatitude = actualLatitude;
}
public Double getActualLongitude() {
return actualLongitude;
}
public void setActualLongitude(Double actualLongitude) {
this.actualLongitude = actualLongitude;
}
public Integer getTrackDirectionAircraft() {
return trackDirectionAircraft;
}
public void setTrackDirectionAircraft(Integer trackDirectionAircraft) {
this.trackDirectionAircraft = trackDirectionAircraft;
}
public Integer getAltitude() {
return altitude;
}
public void setAltitude(Integer altitude) {
this.altitude = altitude;
}
public Integer getSpeed() {
return speed;
}
public void setSpeed(Integer speed) {
this.speed = speed;
}
public Integer getSomeValue1() {
return someValue1;
}
public void setSomeValue1(Integer someValue1) {
this.someValue1 = someValue1;
}
public String getSomeValue2() {
return someValue2;
}
public void setSomeValue2(String someValue2) {
this.someValue2 = someValue2;
}
public String getAirCraftType() {
return airCraftType;
}
public void setAirCraftType(String airCraftType) {
this.airCraftType = airCraftType;
}
public String getRegistrationNumber() {
return registrationNumber;
}
public void setRegistrationNumber(String registrationNumber) {
this.registrationNumber = registrationNumber;
}
public Integer getSomeValue3() {
return someValue3;
}
public void setSomeValue3(Integer someValue3) {
this.someValue3 = someValue3;
}
public String getCountryFrom() {
return countryFrom;
}
public void setCountryFrom(String countryFrom) {
this.countryFrom = countryFrom;
}
public String getCountryTo() {
return countryTo;
}
public void setCountryTo(String countryTo) {
this.countryTo = countryTo;
}
public String getTrackFlight() {
return trackFlight;
}
public void setTrackFlight(String trackFlight) {
this.trackFlight = trackFlight;
}
public Integer getSomeValue4() {
return someValue4;
}
public void setSomeValue4(Integer someValue4) {
this.someValue4 = someValue4;
}
public Integer getSomeValue5() {
return someValue5;
}
public void setSomeValue5(Integer someValue5) {
this.someValue5 = someValue5;
}
public String getAirLine() {
return airLine;
}
public void setAirLine(String airLine) {
this.airLine = airLine;
}
public Integer getSomeValue6() {
return someValue6;
}
public void setSomeValue6(Integer someValue6) {
this.someValue6 = someValue6;
}
public String getAirLineShortName() {
return airLineShortName;
}
public void setAirLineShortName(String airLineShortName) {
this.airLineShortName = airLineShortName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "FlightInfo{" +
"id='" + id + '\'' +
", icao24bitAddr='" + icao24bitAddr + '\'' +
", actualLatitude=" + actualLatitude +
", actualLongitude=" + actualLongitude +
", trackDirectionAircraft=" + trackDirectionAircraft +
", altitude=" + altitude +
", speed=" + speed +
", someValue1=" + someValue1 +
", someValue2='" + someValue2 + '\'' +
", airCraftType='" + airCraftType + '\'' +
", registrationNumber='" + registrationNumber + '\'' +
", someValue3=" + someValue3 +
", countryFrom='" + countryFrom + '\'' +
", countryTo='" + countryTo + '\'' +
", trackFlight='" + trackFlight + '\'' +
", someValue4=" + someValue4 +
", someValue5=" + someValue5 +
", airLine='" + airLine + '\'' +
", someValue6=" + someValue6 +
", airLineShortName='" + airLineShortName + '\'' +
'}';
}
}
Wszystko działa bez zarzutów, ale czy jest to efektywne?
Proszę o pomoc co mógłbym tutaj zmienić.
W niedalekiej przyszłości dodam builder pattern do klasy FlightInfo żeby takiego byka nie było na końcu więc na to new FlightInfo(...)
proszę patrzeć z przymrużeniem oka.