I'm using a API called openweathermap to call for a JSON file that appears in my broswer. I want to grab an element from that JSON file so I wrote this:
URL url = new URL("https://api.openweathermap.org/data/2.5/onecalllat=(lat)&lon=(long)&units=imperial&exclude=hourly,minutely,daily&appid=(apikey)");
URLConnection request = url.openConnection();
request.connect();
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
JsonObject rootobj = root.getAsJsonObject();
JsonElement elem = rootobj.get("current");
System.out.println(elem);
Now as you can tell I am using GSON from Google, I've created a parser object, etc. Here's what the link gets you when you call for the API. Obviously cut out longitude and latitude along with the actual API key for privacy reasons but made sure they're in my code.
"lat": redacted,
"lon": redacted,
"timezone": "redacted",
"timezone_offset": -21600,
"current": {
"dt": 1611308462,
"sunrise": 1611322446,
"sunset": 1611360051,
"temp": 51.39,
"feels_like": 48.61,
"pressure": 1013,
"humidity": 100,
"dew_point": 51.39,
"uvi": 0,
"clouds": 90,
"visibility": 3219,
"wind_speed": 5.75,
"wind_deg": 100,
"weather": [
{
"id": redacted,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04n"
}
]
}
}
I can grab lat, long, timezone, timezone offset, and I've managed to pull the "current" element and print out all of it's content like temp, dt, etc. But I just want to pull "temp" from the "current" section, not all of "currents" contents. How can I do that? Here's what the
System.out.println(elem);
get's you:
{"dt":1611308757,"sunrise":1611322446,"sunset":1611360051,"temp":51.39,"feels_like":48.61,"pressure":1013,"humidity":100,"dew_point":51.39,"uvi":0,"clouds":40,"visibility":6437,"wind_speed":5.75,"wind_deg":160,"weather":[{"id":redacted,"main":"Clouds","description":"scattered clouds","icon":"03n"}]}
What I have tried:
I have tried to pull it as an Array but it didn't work