Context: I'm having trouble getting a value out of a JSON returned by OpenWeatherMap's API via Android.
My JSON looks like this:
{"coord":{"lon":-78.32,"lat":38.55},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"base":"stations","main":{"temp":269.05,"feels_like":265.34,"temp_min":267.59,"temp_max":270.37,"pressure":1010,"humidity":78},"visibility":10000,"wind":{"speed":1.25,"deg":293},"clouds":{"all":25},"dt":1607493640,"sys":{"type":3,"id":2006561,"country":"US","sunrise":1607516385,"sunset":1607550737},"timezone":-18000,"id":4744896,"name":"Ashbys Corner","cod":200}
It is stored in a JsonObject
(Part of GSON, not to be confused with JSONObject
) from a URL like so:
URLConnection requestWeather = weatherUrl.openConnection();
requestWeather.connect(); // connect to the recently opened connection to the OpenWeatherMaps URL
JsonElement parsedJSON = JsonParser.parseReader(new InputStreamReader((InputStream) requestWeather.getContent())); //Convert the input stream of the URL into JSON
JsonObject fetchedJSON = parsedJSON.getAsJsonObject(); // Store the result of the parsed json locally as a json object
Problem: I want to get the value associated with "main"
out of the JSON (which in this case should be "Clouds").
Attempted Solution: I've attempted to get the value of main out like this:
String weatherType = fetchedJSON.get("weather").getAsString();
but this throws a java.lang.UnsupportedOperationException: JsonObject
exception.
Question: How do I get the value of "main"
?