My goal is to parse
a json file with Java.
My json file
looks like this:
{"11542": [40.870932001722714, -73.62889780791781], "54548": [45.859817510232425, -89.82102639934573], "11547": [40.83072033793459, -73.6445076194238]}
What I wanna do is to be able to input the zip code
(the string) and get the coordinates as outcome
Thank you
Edit:
TypeToken helped a lot!!
public class ZipCodeLookup {
public class ZipCodeResult {
final double longitude;
final double latitude;
public ZipCodeResult(double longitude, double latitude) {
this.longitude = longitude;
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
}
public Map<String, Double[]> lookups;
public ZipCodeLookup(InputStream is) throws IOException {
Gson gson = new Gson();
Reader reader = new InputStreamReader(is);
lookups = gson.fromJson(reader, new TypeToken<Map<String, Double[]>>() {
}.getType());
reader.close();
}
public ZipCodeResult lookupZipcode(String zipcode) {
Double[] values = lookups.get(zipcode);
return (values == null) ? null : new ZipCodeResult(values[0], values[1]);
}
}