40 lines
1.2 KiB
Java
40 lines
1.2 KiB
Java
package model;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/*
|
|
Klasa laczaca dostepne nazwy miast i ich koordynaty w mape.
|
|
*/
|
|
|
|
public class JsonToMap {
|
|
public static Map<String, Map<String, Double>> createCityMap(String jsonFile) {
|
|
|
|
Map<String, Map<String, Double>> cityMap = new HashMap<>();
|
|
|
|
try {
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
JsonNode jsonArray = objectMapper.readTree(new File(jsonFile));
|
|
|
|
for (JsonNode jsonNode : jsonArray) {
|
|
String cityName = jsonNode.get("name").asText();
|
|
double latitude = jsonNode.get("latitude").asDouble();
|
|
double longitude = jsonNode.get("longitude").asDouble();
|
|
|
|
Map<String, Double> cordsMap = new HashMap<>();
|
|
cordsMap.put("latitude", latitude);
|
|
cordsMap.put("longitude", longitude);
|
|
|
|
cityMap.put(cityName, cordsMap);
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return cityMap;
|
|
}
|
|
}
|