I am trying to convert a json string into a nested map. here is the json I am trying to convert:
{
"coord": {
"lon": -0.13,
"lat": 51.51
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04n"
}
],
"base": "stations",
"main": {
"temp": 280.83,
"feels_like": 278.79,
"temp_min": 279.15,
"temp_max": 282.15,
"pressure": 1006,
"humidity": 87
},
"visibility": 10000,
"wind": {
"speed": 1.5
},
"clouds": {
"all": 75
},
"dt": 1580086051,
"sys": {
"type": 1,
"id": 1414,
"country": "GB",
"sunrise": 1580111217,
"sunset": 1580143144
},
"timezone": 0,
"id": 2643743,
"name": "London",
"cod": 200
}
what I would like to map into string, float pairs is the "main": {} section with
{
"temp": 280.83,
"feels_like": 278.79,
"temp_min": 279.15,
"temp_max": 282.15,
"pressure": 1006,
"humidity": 87
}
I have also made a WeatherConditions class that is able to convert the "id" and "name" successfully. The WeatherMain class is where I am attempting to fill the map. Here is the code for those classes:
public class WeatherConditions {
int id;
String name;
HashMap<String, Float> main;
public WeatherConditions(int id, String name, HashMap<String, Float> main) {
this.id = id;
this.name = name;
this.main = main;
}
public static class WeatherMain {
private static float temp;
private static float pressure;
private static float humidity;
private static float temp_min;
private static float temp_max;
public WeatherMain( float temp, float pressure, float humidity, float temp_max, float temp_min ) {
WeatherMain.temp = temp;
WeatherMain.pressure = pressure;
WeatherMain.humidity = humidity;
WeatherMain.temp_max = temp_max;
WeatherMain.temp_min = temp_min;
}
public static String displayWeatherMain() {
String temperature;
temperature = "Temp: " + temp + "\nPressure: " + pressure + "\nHumidity: " + humidity +
"\nTemp_max: " + temp_max + "\nTemp_min: " + temp_min;
return temperature;
}
}
public String displayWeatherConditions() {
String temp;
temp = "ID: " + id + "\nName: " + name + "\n" + WeatherMain.displayWeatherMain();
return temp;
}
}
In the main class I use this this function to parse the json string:
public static String parse(String responseBody) {
Gson gson = new Gson();
WeatherConditions newWeather = gson.fromJson(responseBody, WeatherConditions.class);
System.out.println(newWeather.displayWeatherConditions());
return null;
}
my display is able to get the correct id and name but just gives me zeros for all the other values I am trying to convert. What is the correct way to do this?