54 lines
1.7 KiB
Java
54 lines
1.7 KiB
Java
package org.adamgulczynski;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
public class JsonReader {
|
|
private Map<String, Map<String, Double>> dataMap;
|
|
|
|
public Map<String, Map<String, Double>> readFile(String filePath) {
|
|
// Create a map to store the data
|
|
Map<String, Map<String, Double>> cityDataMap = new HashMap<>();
|
|
|
|
try {
|
|
// Create ObjectMapper
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
// Read JSON array from file into JsonNode
|
|
JsonNode jsonArray = objectMapper.readTree(new File(filePath));
|
|
|
|
for (JsonNode jsonNode : jsonArray) {
|
|
String city = jsonNode.get("city").asText();
|
|
double lat = jsonNode.get("lat").asDouble();
|
|
double lon = jsonNode.get("lon").asDouble();
|
|
|
|
// Create a map for latitude and longitude
|
|
Map<String, Double> latLngMap = new HashMap<>();
|
|
latLngMap.put("latitude", lat);
|
|
latLngMap.put("longitude", lon);
|
|
|
|
// Put the data into the main map
|
|
cityDataMap.put(city, latLngMap);
|
|
}
|
|
this.dataMap = cityDataMap;
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return cityDataMap;
|
|
}
|
|
|
|
public void printEntries() {
|
|
short index = 1;
|
|
Set<String> keys = this.dataMap.keySet();
|
|
for (String key : keys) {
|
|
System.out.println(index + ". " + key);
|
|
index++;
|
|
}
|
|
}
|
|
} |